Python Python Data Structures

f-strings, new way to format strings in Python

We are using different methods to format a string in Python, like using string literal or using a format method of string.
In Python 3.6 or more, f-strings is a new efficient way to format strings. They are more readable and less prone to errors than other ways of formatting.

Old Methods of String Formatting in Python

Using string literal

>>> name = "Anurag"
>>> print("My name is %s" % name)
>>> 'My name is Anurag'

In this case, %s is the placeholder which is getting replaced by the variable’s value mentioned after % at the end of string.

To insert more than one variable, we must need to use a tuple of those variables.

>>> first_name = "Anurag"
>>> last_name = "Choudhary"
>>> print("My name is %s %s" %(first_name, last_name))
>>> 'My name is Anurag Choudhary'

Now the biggest disadvantage of this, like if we need to use the same variable, again and again, we need to put that in the tuple again which increases the unnecessary code and also readability is also not good.

Also, it is mandatory to put the variable in the same order, in which the string literal are defined in the string.

For Example:

>>> first_name = "Anurag"
>>> print("Hi %s. Welcome %s" %(first_name, first_name))
>>> 'Hi Anurag. Welcome Anurag'

 

Using .format() method

This is the better way to use formatting in string compare to `string literals` mentioned above. This has been introduced in Python 2.6.

In str.format(), the placeholders are marked by using the curly braces.

>>> first_name = "Anurag"
>>> last_name = "Choudhary"
>>> print("My name is {} {}".format(first_name, last_name))
>>> 'My name is Anurag'

A reference variable can be accessed by using their index:

>>> first_name = "Anurag"
>>> last_name = "Choudhary"
>>> print("My name is {0} {1}".format(first_name, last_name))
>>> 'My name is Anurag'

It can also be accessed by names:

>>> first_name = "Anurag"
>>> last_name = "Choudhary"
>>> print("My name is {first} {second}".format(first=first_name, second=last_name))
>>> 'My name is Anurag'

The major issue with .format() is the code readability, it will create a longer line of code in the case of multiple parameters.

>>> first_name = "Anurag"
>>> last_name = "Choudhary"
>>> city = "Gurugram"
>>> state = "Haryana"
>>> country = "India"
>>> print("My name is {first} {last}. And I live in {city}, {state} {country}".format(first=first_name, 
                                                                                      last=last_name, 
                                                                                      city=city, 
                                                                                      state=state, 
                                                                                      country=country))
>>> 'My name is Anurag Choudhary. And I live in Gurugram, Haryana India

 

f-strings

It is a new way to format the strings, introduced in Python 3.6. Much more readable and less error prone.

>>> name = "Anurag"
>>> print(f"My name is {name}")
>>> 'My name is Anurag'

In f-strings, we need to put “f” in front of the string and the placeholder ( curly braces ) with the variable name. We can directory use the variable name in the placeholder.

 

f-strings comes with much more power. We can perform some actions on the string as well.

Like using .upper() method of the string.

>>> name = "anurag"
>>> print(f"My name is {name.upper()}")
>>> 'My name is ANURAG'

 

Performing Calculations:

>>> print(f"2 time 5 is {2 * 5}")
2 time 5 is 10

 

Multiline f-strings:

>>> name = "Anurag"
>>> profession = "Software Developer"
>>> msg = (
...     f"My name is {name}. "
...     f"I am a {profession}. ")
>>> print(msg)
>>> 'My name is Anurag. I am a Software Developer.'

 

Inspect Variable using f-strings:

We just need to add {variable=} inside the f-string.

>>> name = "anurag"
>>> country = "India"
>>> print(f"{name=} {country=}")
name='anurag' country='India'