F-strings, also known as f-literals, is a new and improved way of formatting strings in Python, starting from version 3.6. The main advantage of using f-strings over other string formatting methods, such as %-formatting or str.format(), is that f-strings are more readable, concise, and efficient. In this article, we will go through the basics of f-strings, how to use them, and why they are a better choice for string formatting in Python.
What are F-Strings?
F-strings are a string literal that is prefixed with the letter “f”. They allow embedding expressions inside string literals, using {} brackets. The expressions are evaluated at runtime and their values are concatenated into the string. This makes it possible to create dynamic and flexible strings that are easier to read and maintain than traditional string formatting methods.

Example:
name = "Mike"
print(f"My name is {name}")
Output:
My name is Mike
Let’s compare this to the old-school approach using the format() method:
name = "Mike"
print("My name is {}".format(name))
This cleans up the code and makes it simpler for developers to read. Furthermore, adding multiple variables is not as much of a problem now:
first_name = "Mike"
last_name = "Mcalister"
profession = "Developer"
platform = "google.com"
print(f"Hi! I am {first_name} {last_name}, a {profession}. I'm writing a new article on {platform}.")
Output:
Hi! I am Mike Mcallister, a Developer. I'm writing a new code on google.com
Now that you know how to use F-strings in Python, let’s take a look at some other commonly used string formatting options.
Arbitrary Expressions and Formatted Strings
Arbitrary expressions, also known as f-strings, can be formatted with expressions inside braces. This type of string formatting allows for easy and efficient customization when printing information differently. For example, the contents of a variable could easily be printed within an f-string without manually constructing the output string.
You can put any valid Python expression inside an F-string.
For example, let’s say we want to include the result of a calculation in a string. We can do this by using an F-string:
To do this:
- You will need to break the string into multiple lines.
- Then, convert each line to a separate F-string.
Example:
first_name = "Mike"
last_name = "Mcallister"
profession = "Developer"
platform = "Google"
message = (
f"Hi! I am {first_name} {last_name}."
f"I'm a {profession}."
f"I'm developer on {platform}."
)
print(message)
Hi! I am Nick Jones, a Software Engineer. I'm writing a new article on Codingem.com.
F-String Table in Python
You can tailor how many spaces each variable within your F-string occupies.
You can do this by adding :{} after the variable name inside the F-string and then determining the number of spaces parameterized inside the curly braces.
One application for this might be if you want your data to appear tabular.
Example:
data = [("x", "y", "sum"), (1, 2, 3), (3, 5, 8)]
for x, y, sum in data:
print(f"{x:{1}} {y:{1}} {sum:{2}}")
Output:
x y sum
1 2 3
3 5 8
F-String Decimal Places in Python
F-String Decimal Places in Python is a feature of the “f-strings” (formatted strings) formatting style that allows you to specify how many decimal places you want to display when printing out an expression. This can be useful if you are dealing with numbers with many digits or wish to make your output look more precise.
Example: if you wanted to round 3.1415926 to two decimal places, you could do so like this:
f_num = 3.1415926
print(f"The number is: {f_num:.2f}")
Output:
The number is: 3.14
Wrap up
F-String Decimal Places in Python is a simple and efficient way to ensure your output looks as precise as you need. You can quickly add decimal places or round up the numbers in your code with just a couple of characters. This way, you can ensure that your results are accurate while keeping your code readable and concise.
F-strings are much better than format() when working with multiple variables because the code looks neater. In addition, with F-strings, you can place the variable precisely where in the text you want it to appear.
Thanks for reading. Happy coding!