Home » F String in Python

F String in Python

In this tutorial, we will learn about the string formatting mechanism. The f-string is the best way to format the string. The string’s formatting style makes the string more readable, more concise, and less prone to error. It is also faster than the other.

Before diving deep into this topic, we will first discuss the techniques were used before the f string.

Old String Formatting in Python

There are two main ways for formatting the string: % – formatting and str.format(). But both methods have some limitations. Let’s have a brief introduction of these given methods.

1.% – formatting

It is a traditional way of formatting the string, but the docs recommend this method, which contains the following statement.

“The formatting operations described here exhibit a variety of quirks that lead to several common errors (such as failing to display tuples and dictionaries correctly).

Using the newer formatted string literals or the str.format() interface helps avoid these errors.” – Official Documentation.

How to use % – formatting

We can perform built-in operation using the %-operator. Let’s understand the following example.

Output:

Hello Michel  

We can also use the tuple to store the multiple variables. Let’s see the following example.

Output:

Hello Sharma You are 24.  

Why %-formatting is not recommended

Because as the variable is increased and a longer string, our code becomes much less easily readable. The code looks messy. Let’s see the following example.

Example –

Output:

Hello, Steve Rogers. Your age is 70. You are a Superhero. You were a member of Marvel.  

As we can see in the above code, the code became hard to read and more prone to the error. That’s why this way of formatting string isn’t good.

2.str.format() method

It is another popular way to format the string introduced in Python 2.6. Let’s see how we can use it.

How to Use str.format()

It is an improvement on %-formatting. It is like a normal built-in function called on the object and that object being converted to a string.

Let’s see the following example.

Example –

To access the dictionary element inside the format method, we can pass the key in the format() method.

Output:

Hello Peter, You are 17  

Why str.format() method is not recommended?

The str.format() is much efficient than the %-format method but it can be still be quite verbose when we deal with the multiple parameters.

F-string Method

It is a new string formatting mechanism introduced by the PEP 498. It is also known as Literal String Interpolation or more commonly as F-strings (f character preceding the string literal). The primary focus of this mechanism is to make the interpolation easier.

When we prefix the string with the letter ‘F, the string becomes the f-string itself. The f-string can be formatted in much same as the str.format() method. The F-string offers a convenient way to embed Python expression inside string literals for formatting.

Example –

Output:

Hello, My name is Tushar and I'm 28 years old.  

In the above code, we have used the f-string to format the string. It evaluates at runtime; we can put all valid Python expressions in them.

We can use it in a single statement.

Output:

60  

However, we could use in function.

Output:

Sachin Tendulkar is great  

The f-string could also be used with the class object. Let’s understand the following example.

Example –

Output:

Keenu Reevs's superhit movie is Matrix.  

Explanation –

In the above code, we have used the __str__() and __repr__(), representing an object as a string. So we need to include at least one of those methods in the class definition. The f-string will use the __str__() method; we can also use the __repr__() by including the conversion flag ! r.

Output:

Keenu Reevs's superhit movie is Matrix.  Keenu Reevs Matrix Superhit  

F-string in Dictionary

We have to take care while working with dictionary keys inside the f-string. There is a different quotation to use dictionary keys and f-string. Let’s understand the following example.

Example –

Output:

John is 19 years old.  

Below method is not allowed in case of dictionary.

Example –

Output:

File "", line 2      print(f'{detail['name']} is {detail['age']} years old.')                       ^  SyntaxError: invalid syntax  

As we can see in the above code, we change a double quotation to single quotes, and it has thrown an error.

Speed

The reason for adapting this formatting style is its speed. The f-string evaluates at runtime rather than constant values. It embeds expression inside string literals, using minimal syntax. It is fast because it evaluates at runtime, not a constant value.

Let’s see the following string comparison.

Example – 1:

Output:

0.0022497819736599922  

Example – 2:

Output:

0.0025783719611354172  

Example – 3:

Output:

0.0019360429723747075  

As we can observe, the f-string is on the top of list.

Braces

To make appear braces in the code, you should use the double quotes as follows. Let’s understand the following example.

Example –

Output:

{70 + 40}  

If we use the triple braces, it will display single braces in our string. Let’s understand the following example.

Example –

Output:

{94}  

We can display the more braces if we use more than triple braces.

Example –

Output:

{{70 + 4}}  

Backslashes

We can use the backslash escapes in the string portion of an f-string. However, we can’t use backslashes to escape in the expression part of an f-string. Let’s understand the following example.

Example –

Output:

SyntaxError: f-string expression part cannot include a backslash  

Inline comments

We cannot include the # symbol in the expression. It will throw a syntax error. Let’s understand the following example.

Example –

Output:

SyntaxError: f-string expression part cannot include '#'  

Conclusion

We can use any one method out of three, but the f-string method provides a more concise, readable, and convenient way. It is faster and less prone to error. We have explained almost every possible scenario off-string and why one should consider this approach in programming.


Next TopicHow Brython Works

You may also like