f-strings are new in Python 3.6.
f-strings look like an ordinary strings with an f in front. They look like an ordinary (immutable) string that does not change, but they are not immutable strings.
One can put expressions within curly braces in an f-string, and the expressions will be evaluated each time, and then converted to a string. The most common expressions used are just variable names.
It is a more DRY way of doing formatting than str.format.
So avoid covering complicated stuff and edge cases:
Let's look at how formatting is often done with the str.format method, then see how formatting is done with f-strings.
stuff = {
'apple': 1.97,
'banana': 2.99,
'cherry': 3.99,
}
# Common pattern of .format use: use numerical indexes
for name, price in stuff.items():
print('The price of {0} is {1}.'.format(name, price))
The price of apple is 1.97. The price of banana is 2.99. The price of cherry is 3.99.
In 'The price of {0} is {1}' above, one must read back and forth to match the numerical indexes in the string with the .format method arguments. Having to figure out what the numerical indexes are for hurts readability.
Which leads to a better way below using named arguments to the .format method so that meaning is obvious in the format string.
# Common pattern of .format use: use parameter names
for name, price in stuff.items():
print(
'The price of {name} is {price}.'.
format(name=name, price=price))
The price of apple is 1.97. The price of banana is 2.99. The price of cherry is 3.99.
Something that sucks about the above print, is that name and price appear three times each. I.e., It is not DRY.
With f-strings, name and price only have to appear once, making the code easier to read and maintain as in the following example.
for name, price in stuff.items():
print(f'The price of {name} is {price}.')
The price of apple is 1.97. The price of banana is 2.99. The price of cherry is 3.99.
It reminds me of shell syntax. For example,
echo "The price of ${name} is ${price}"
It gets better. One may put expressions within the curly braces in an f-string. One is not limited to using just variable names.
tax_rate = 0.50
for name, price in stuff.items():
print(f'The total price of {name} is {round(price * (1+tax_rate), 2)}.')
The total price of apple is 2.96. The total price of banana is 4.49. The total price of cherry is 5.99.
But what does round(price * (1+tax_rate), 2) mean? What is it for? Just because you can put expressions in a f-string does not mean that you should.
In the cell below, a temporary variable is used for documentation to make the code easier to read.
tax_rate = 0.50
for name, price in stuff.items():
total_price = round(price * (1+tax_rate), 2)
print(f'The total price of {name} is {total_price}.')
The total price of apple is 2.96. The total price of banana is 4.49. The total price of cherry is 5.99.