Sub topic 9
Quick debugging with f"{x=}"
This chapter assumes you can write f-strings with replacement fields, know the difference between str() and repr(), and can run a short Python script. The = specifier needs Python 3.8 or later.
What the = specifier does
When you add = after an expression inside an f-string, Python prints the expression text, an equals sign, and then the value. You don't have to type the name twice.
x = 7
print(f"{x=}")
print(f"{x * 2 = }")This prints:
x=7
x * 2 = 14The first line shows the basic form. The second line shows that the expression can be any valid expression, not just a name. Python copies the source text exactly as you wrote it, including spaces. Because of that, {x * 2 = } prints with spaces around the equals sign, and {x*2=} prints without them.
Why it uses repr() by default
With =, Python formats the value with repr() unless you give a conversion or a format spec. This is useful for debugging because it shows quotes and escape sequences.
name = "Ada"
text = "tab\there"
items = [1, 2, 3]
print(f"{name=}")
print(f"{text=}")
print(f"{items=}")Output:
name='Ada'
text='tab\there'
items=[1, 2, 3]Without =, f"{name}" would print Ada with no quotes. With =, you can see that name is a string, and you can see hidden characters such as the tab.
Combining = with conversions and format specs
You can add a conversion such as !s, or a format spec after a colon. Write = first, then the conversion, then the spec.
price = 1234.5
name = "Ada"
print(f"{price=:.2f}")
print(f"{price=:,.1f}")
print(f"{name=!s}")
print(f"{name=!r:>10}")Output:
price=1234.50
price=1,234.5
name=Ada
name='Ada'The last line is padded to a width of 10 after the expression text. Two rules apply:
- If you give a format spec and no conversion, Python formats the value with that spec instead of
repr(). That is whyprice=:.2fshows1234.50. - If you give a conversion, such as
!s, Python applies it before formatting.
Where it helps and where it doesn't
The = form is good for quick checks while you work, for example inside a loop:
total = 0
for n in [4, 5, 6]:
total += n
print(f"{n=} {total=}")Output:
n=4 total=4
n=5 total=9
n=6 total=15Keep these limits in mind:
- It is a debugging aid. For user-facing output, write the label yourself so you control the wording.
- The label is the source text, so a long expression makes a long label.
- Calls with side effects run once per field.
f"{pop()=}"removes an item each time the f-string is evaluated. - It writes to wherever you print. For debug messages that should not mix with results, send them to
sys.stderrwithprint(..., file=sys.stderr).
Exercise
Exercise
What does this code print? Explain each line. label = "ok" n = 3 print(f"{label=}") print(f"{n + 1=}") print(f"{n=:03d}")
Show answer
It prints three lines: label='ok' n + 1=4 n=003 The first uses repr() by default, so the string shows quotes. The second prints the expression text exactly as written, with no spaces because none were typed, then its value. The third has a format spec and no conversion, so the value is formatted with 03d, which pads to width 3 with zeros.