Python f-strings: fun strings, fast strings

Gus Goulart
Jul 25, 2017 · 2 min read

The last Python major release(3.6) is the new joy of Python’s community. Even the language luminaries are super excited, as you can see.

My favorite improvement, (and the first I’ve used) is the Literal String Interpolation, A.K.A f-strings. What they do is simply beautiful. Take a look:

>>> import datetime
>>> today = datetime.datetime.now().date()
>>> print(f'Today is {today}')
Today is 2017-07-22

And what about this:

>>> import datetime
>>> print(f'Today is {datetime.datetime.now().date()}')
Today is 2017-07-22

Or this:

>>> price = 1/3
>>> price
0.3333333333333333
>>> print(f"The price is {price:.03f}")
The price is 0.33

Maybe:

>>> user = dict(name='Augusto', age=25, favorite_language='Python')
>>> print(f"The user {user.get('name')} is {user.get('age')} years old and likes to code in {user.get('favorite_language')}")
The user Augusto is 25 years old and likes to code in Python

Using f-strings is really straight forward. Simply define a normal string prefixed with “f”, and the Python interpreter will look for the name inside the curly braces in the local scope or execute the passed expression.

It’s worth saying that f-strings aren’t a new data type, just a new way to define a string:

>>> ans_for_life = "42"
>>> type(ans_for_life)
<class 'str'>
>>> the_answer= f"Python is {ans_for_life}"
>>> the_answer
'Python is 42'
>>> type(the_answer)
<class 'str'>

F-strings aren’t just more Pythonic, but also faster. Let’s create a simple benchmark comparing with the popular str.format() method.

I’m running the script on a Lenovo Thinkpad T440p, dual core i5 4300m 2.60 GHz, 16gb of RAM, 250gb SDD and Linux Mint 18.2. The results were:

f-strings:  75.75738063999961

.format(): 250.012545511001

What we have here are one billion strings substitutions, and in this simple scenario f-strings are almost 3.5x faster than str.format().

Every technical improvement in the open source community comes from a person or a group. Eric V. Smith is the name behind f-strings. He is the author of the Python enhancement proposal 498 from August 2015. Quoting him:

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with ‘f’, which contains expressions inside braces. The expressions are replaced with their values.

It goes without saying that f-strings are restricted to projects using Python 3.6 and above only. So if your team is using some older version, stay with the traditional string formatting tools. Although if your team is using Python 3.6+, f-strings are totally optional, and every single string formatting code you’ve used is still valid.

This article was originally posted on my blog: f-strings-fun-strings-fast-string.html

https://augustogoulart.me. I help people and their business. Usually, I use Python and Django for that.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade