F String in Python
Last Updated : 17 Mar 2025
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 look into what is f-string and the techniques were used before the f string.
What is F-string?
An f-string (formatted string literal) in Python is a way to embed expressions inside string literals using curly braces {}. Introduced in Python 3.6, f-strings provide a concise and readable way to include the value of Python expressions in strings.
Key Features of f-strings:
- Embedding Expressions: You can embed any valid Python expression inside curly braces {} within an f-string.
- Improved Readability: f-strings are more readable and concise compared to other string formatting methods like str.format() or the % operator.
- Efficient: f-strings are faster than other string formatting methods because they are evaluated at runtime.
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
Old string formatting in Python refers to using the % operator, which is similar to the printf-style formatting found in C. This method has been largely replaced by more modern techniques like str.format() and f-strings, but it is still widely used and understood.
"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:
We can also use the tuple to store the multiple variables. Let's see the following example.
Output:
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
The str.format() method in Python is a more flexible and powerful string formatting technique compared to the old % operator. Introduced in Python 2.7 and 3.0, str.format() allows for more complex and readable formatting of strings.
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 -
Using Variables:
Accessing Dictionary Elements:
Output:
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.
.The f-string method, introduced in Python 3.6, provides a concise and efficient way to embed expressions inside string literals using curly braces {}. F-strings (formatted string literals) enhance readability and are faster compared to other string formatting methods.
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:
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:
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:
Example - 2:
Output:
Example - 3:
Output:
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:
If we use the triple braces, it will display single braces in our string. Let's understand the following example.
Example -
Output:
We can display the more braces if we use more than triple braces.
Example -
Output:
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.