F-Strings in Python: What They Are and How to Use Them

Sophia Iroegbu - Aug 27 - - Dev Community

F-strings (Formatted strings) in Python are an easy way to add expressions in Strings. It was first introduced in Python 3.6, making string formatting much more readable and easy.

In this guide, we will understand f-strings and why they are so much better than the standard formatting method, str.format().

What are F-strings?

F-string is a formatting technique introduced to Python that makes it so much easier to play around with expressions and strings; it is usually defined by the prefix, f, and using the curly brackets, {} to format your value or to interpolate.

The usefulness of this type of string formatting lies in the following:

  • Readability: F-strings lets you embed expressions directly within the string, which makes it easy to read and understand.

  • Performance: F-strings are faster than your average string formatting, like str.format() or % because f-strings do not need any overhead of method calls.

  • Easy to Use: With f-strings, you do not need to remember different formatting methods or symbols; you only need to prefix your string.

  • Flexibility: When using f-strings, expressions defined can include any valid Python expression inside the curly brackets an not just variables.

  • Type safety: F-strings automatically convert your expressions to strings, thereby reducing the chance of type conversion errors.

These are a few reasons you should consider f-strings when dealing with formatting. Let's look at some code examples to show how this awesome formatting method works.

What is str.format() all about?

str.format() is the OG formatting method. This method is still useful when you need to reuse your format technique more than once with different values or data types or if you are working on a project that uses Python versions earlier than 3.6.

Aside from using it with older Python version projects, str.format() is also useful when dealing with more advanced formatting options like padding, number formatting, alignment, etc, or when formatting dictionaries or objects, making dealing with structured data so easy.

While this method is useful, it is also very slow because of many function calls when formatting values. str.format() is just as useful as f-strings, depending on your use case.

Let's look at a code example of how str.format() works:

name = "Sophia"
age = 30

# Using str.format() to format the string
formatted_string = "My name is {} and I am {} years old.".format(name, age)

print(formatted_string)
Enter fullscreen mode Exit fullscreen mode

Your response will look like this:

My name is Sophia and I am 30 years old.

Enter fullscreen mode Exit fullscreen mode

Basic Syntax of F-string

As stated earlier, f-strings are prefixed f before the string, and inside the string, you add the variable or the expression within curly brackets, {}.

Here's a basic syntax that shows how variables can be defined within the f-string.

name = "Sophia"
country = "Nigeria"

greetings = f"Hello, I am {name.capitalize()} and I am {country}"
print(greetings)
Enter fullscreen mode Exit fullscreen mode

Your response will look like this:

I am Sophia and I am from Nigeria
Enter fullscreen mode Exit fullscreen mode

Adding Expressions within F-strings

Now, let’s look at adding expressions inside f-strings. You can also add the expressions within curly brackets.

Here’s a code example:

a = 10
b = 8

result = f"The sum of {a} and {b} is {a + b}"
print(result)
Enter fullscreen mode Exit fullscreen mode

Your response will look like this:

The sum of 10 and 24 is 34
Enter fullscreen mode Exit fullscreen mode

Formatting Numbers within F-strings

Let’s take another look at how we can use f-strings. You can format numbers within the curly braces.

Here's a code example:

value = 500.287018 

formatted_value = f"The {value} in two decimal places is {value:.2f}"
print(formatted_value)
Enter fullscreen mode Exit fullscreen mode

Your response will look like this:

The 500.287018 in two decimal places is 500.29
Enter fullscreen mode Exit fullscreen mode

Using F-strings in multiple lines

Lastly, let’s see if the f-strings can be used in multiple lines using triple quotes.

Here's a code example:

name = "Mercedes"
year = "2009"

result = f"""
Name : {name}
Year the car was made: {year}
"""
print(result)
Enter fullscreen mode Exit fullscreen mode

Your response will look like this:


Name : Mercedes
Year the car was made: 2009

Enter fullscreen mode Exit fullscreen mode

Conclusion

Voila! Congrats you now understand what f-strings are and how this is a great formatting method for your projects.

In this guide, I focused on formatting strings and numbers, but f-strings can be used to format all data types in Python.

If you prefer the video version of this guide, check it out:

. . . . . . . . . . .