What are Python f-strings?

With Python f-strings, you can insert complex expressions and variables directly into a string, without having to perform additional conversions or formatting.

What can Python f-strings do?

Python f-strings are a Python string format that were introduced with Python 3.6. They’re also known as formatted string literals. Within an f-string, you can evaluate calculations using curly brackets. F-strings allow more compact notation than other string formats like str.format() and concatenation with +. This ultimately leaves you with clearer and more concise code.

F-strings are highly flexible and allow you to insert various data types like integers, floats, lists, expressions and functions without converting them or adding special formatting. Python f-strings tend to be faster than other string formatting methods in Python.

What is the syntax of Python f-strings?

The basic structure of a Python f-string consists of the prefix f or F, followed by a string in quotation marks (“ or ’). Within the string, you can use curly brackets {} to embed variables and expressions.

name = "Peter"
age = 25
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string) # Output: My name is Peter and I am 25 years old.
python

In this example, we put the variables name and age in curly brackets in the f-string –{name} and {age}.

Web Hosting
Secure, reliable hosting for your website
  • 99.9% uptime and super-fast loading
  • Advanced security features
  • Domain and email included

How are Python f-strings used?

F-strings can be used in a variety of ways.

Performing calculations within a string

Python f-strings are especially practical if you want to do arithmetic calculations within a string. They allow you to define a complex expression in a single line of code.

num1 = 10
num2 = 5
result = f"The sum of {num1} and {num2} is {num1 + num2}"
print(result) # Output: The sum of 10 and 5 is 15
python

In this example, num1 and num2 are added within the f-string. The result is shown in the final string output.

Python f-strings and raw strings

The combination of r for raw strings and f for f-strings creates a special kind of string in Python, known as an rf-string. With rf-strings, you can combine the functionality of raw strings with the flexibility of f-strings. R-strings interpret escape sequences literallyand f-strings can directly embed variables and expressions in a string.

name = "User"
path = rf'C:\Users\Username\Documents\{name}\file.txt'
print(path) # Output: C:\Users\Username\Documents\User\file.txt
python

In the above code, we used an rf-string to define a path. We used {name} in the rf-string to directly insert the variable name. Meanwhile, the r before the string ensures that the backslash \ is treated as a literal character and not part of an escape sequence.

Precision of floating numbers

You can use special formatting statements to specify the number of decimal places in a floating number in an f-string.

value = 3.14159265359
formatted_value = f"Rounded value = {value:.3f}"
print(formatted_value) # Output: Rounded value = 3.142
python

In the above code, the formatting statement :.3f indicates that the variable value should be rounded to three decimal places and inserted into the formatted string.

Width and alignment

In Python f-strings, width and alignment specifications allow you to control how values are displayed within a field. This is useful for placing text and numbers in a specific place and aligning them left, right or centre.

Say we have the name __Alice__ and want to right-align it in a field that’s 10 characters in width.

name = "Alice"
formatted_name = f"Hello, {name:>10}"
print(formatted_name) # Output: Hello,      Alice
python

Since the value is less than 10 characters long, {name:>10} will right-align it.

Filling with zeros or other characters

Filling with zeros or other characters influences the alignment of numbers in a field. This is useful if you want numbers in a fixed format, such as when you’re displaying times or numerical values.

number = 42
formatted_number = f"Number: {number:06}"
print(formatted_number) # Output: Number: 000042
python

In this example, :06 indicates that the number for number will be entered in a six-digit field and any missing digits will be filled in with leading zeros.

You can also use characters other than zeros to fill a string:

word = "Python"
formatted_word = f"Word: {word:_<10}"
print(formatted_word) # Output: Word: Python____
python
Was this article helpful?
Page top