Skip to content
Theory · Strings · lesson 7 of 10

f-strings

Picture a form with blanks: "Hi, ____. Total: ____". You write a name and an amount into the blanks and get finished text. The form itself stays the same for any customer.

In Python, such a form is called an f-string. Its blanks hold names and expressions, and Python writes their values in for you.

Without a form, a string with numbers is built with +. Then every number has to be turned into text with str(), and the spaces must be remembered inside the quotes. Compare the two ways:

Python
name = "Anna"
total = 1250
print("Hi, " + name + ". Total: " + str(total))  # Hi, Anna. Total: 1250
print(f"Hi, {name}. Total: {total}")  # Hi, Anna. Total: 1250

Let's go through both lines:

  1. The first print joins four pieces with +, and turns the number total into text with str(total) first.
  2. The second print writes the same text as one f-string: the values stand right where they will appear in the text.

Replacement fields

An f-string is a string with the letter f before its opening quote. The curly braces in it are a replacement field, the blank in the form. Python evaluates what stands inside, turns the result into text and puts it in place of the braces. The result is an ordinary string of type str.

The braces can hold any expression:

  • arithmetic: {qty * price};
  • a function call: {len(name)};
  • a method call: {name.upper()};
  • an index or a slice: f"{name[0]}. {name[1:]}" gives A. nna.
Python
price = 19.5
qty = 3
print(f"{qty} x {price} = {qty * price}")  # 3 x 19.5 = 58.5
print(f"{name.upper()} has {len(name)} letters")  # ANNA has 4 letters

The expression is evaluated once—when execution reaches the f-string. If qty changes later, the finished string stays as it was.

Here the form analogy ends. A paper form can be filled in again for another customer. A finished f-string has no blanks left: it is an ordinary string with the text already written in. To get text with new values, the f-string runs again.

Format after the colon

After the expression, a field can hold a colon and a format spec—a rule for how to show the value. Two kinds are needed most often.

Digits after the point

.2f shows a number with two digits after the point and rounds it. The f here stands for fixed-point notation and has nothing to do with the letter before the quote. Other variants:

  • .0f rounds to a whole number and prints no point;
  • an integer with .1f gets a fractional part: 42.0.

Width and alignment

>5 right-aligns the value, padding it with spaces to a width of 5, and <5 left-aligns it. A value longer than the given width is not cut: f"{12345:>3}" gives 12345. This is how even columns are printed in tables and receipts.

Python
pi = 3.14159
n = 42
print(f"{pi:.2f} {pi:.0f} {n:.1f}")  # 3.14 3 42.0
print(f"[{n:>5}] [{n:<5}]")  # [   42] [42   ]
print(f"[{pi * 2:>8.3f}]")  # [   6.283]

A format spec changes only the text: the variable pi is still 3.14159. It also applies to the result of an expression, as in the last line:

  1. First pi * 2 is evaluated.
  2. Then the number is rounded to three digits after the point.
  3. Finally, it is right-aligned in a field of width 8.

To print a curly brace itself, double it: f"{{n}}" gives {n}.

Common mistake

The letter f before the quote is missing:

Python
total = 1250
print("Total: {total}")  # ✗

There is no error, but Total: {total} is printed. Without the f, curly braces are ordinary characters of the string, and Python substitutes nothing into them. The fix is to add the letter: f"Total: {total}" gives Total: 1250.

Key points

  • An f-string starts with the letter f, and Python evaluates the expressions in {} and puts them into the text.
  • The expression is evaluated once, when execution reaches the f-string.
  • A format follows the colon: .2f is two digits after the point, >5 is width 5, right-aligned.
  • Without the letter f, curly braces stay ordinary characters.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way