What is the difference between f-string and str.format() in Python?

  • What is the new string interpolation/formatting style has been introduced in Python 3.6?
  • Is better or worse than str.format() commonly used these days?
  • Should one start using f-String now that Python has introduced it recently as str.format() may be deprecated in the newer versions of Python?

Before getting into the new-style of formatting strings in Python known as f-Strings, let us first discuss how str.format() works. str.format() was introduced in Python 2.6 and is an improvement on %-formatting (which though not deprecated but is not recommended anymore).
The replacement fields in str.format() are marked by curly brackets, {}.

Input:

name = 'Tanya'
place = 'Delhi, India'
print('I am {}. I am from {}.' .format(name, place))
# variables can be referenced in any order by using the referencing index
print('I am {1}. I am from {0}.' .format(place, name))

Output:

I am Tanya. I am from Delhi, India.
I am Tanya. I am from Delhi, India.


We can also use dictionaries:

Input:

info = {'name': 'Tanya', 'place': 'Delhi, India'}
print('I am {name}. I am from {place}.' 
      .format(name=info['name'],  place=info['place]))
print('I am {name}. I am from {place}.' .format(**info)

Output:

I am Tanya. I am from Delhi, India.
I am Tanya. I am from Delhi, India.


Even though it might not seem too verbose or wordy but when when there are too many variables, or too many keys in a dictionary, str.format() way of formatting or string interpolation gets verbose.

In Python 3.6, f-Strings were introduced which tackled the above issue and also increased readability of code. This new way prefixes the string constant with the letter f' ' or F' ' . The syntax can be understood using the following example.

Input:

name = 'Tanya'
place = 'Delhi, India'
string = f'I am {name}. I am from {place}.'
print(string)

Output:

I am Tanya. I am from Delhi, India.


Also, we can put expression inside the curly brackets as well.

Input:

a = 23
b = 45
sum = f'The sum of {a} and {b} is {a+b}.'
print(sum)

Output:

The sum of 23 and 45 is 68.


f-Strings also allow multi-line strings by using f'''...''' instead of f' ' and just pressing enter where we want to break lines instead of using \n . Also, Python will thrown a SyntaxError if backslash i.e. \ is included in f-String expression.

Input:

name = 'Tanya'
place = 'Delhi, India'
string = f'''I am {name}.
I am from {place}.'''
print(string)

Output:

I am Tanya.
I am from Delhi, India.


In terms of speed, f-Strings beats str.format(). Let us look at the comparison.

Input:

import timeit
timeit.timeit("""name = 'Tanya'
place = 'Delhi, India'
'I am {}. I am from {}.' .format(name, place)""", number = 10000)

Output:

0.0035459260000010318


Input:

import timeit
timeit.timeit("""name = 'Tanya'
place = 'Delhi, India'
f'I am {name}. I am from {place}.' """, number = 10000)

Output:

0.002438100000006216



As seen above, f-Strings are faster than str.format(). str.format() allows the use of \ in its expression whereas f-Strings does not. There are cases where f-Strings are better but other cases where it would be better to use str.format().


Also, in my opinion, you do not need to worry about Python deprecating str.format() anytime soon, as it still has not yet deprecated % -formatting till now.


With that said, one should incorporate new styles and methods into their coding practices, so whenever you get a chance to do so, never say F'No!!'.

1 Like