Mastery
Mastery/Python/E. Typing & modern syntax
T1 · high-leverage

f-string = debug specifier and the format-spec mini-language

f"{expr=}" prints the source text of the expression, an =, and its repr() -- built specifically for quick debugging prints without manually typing the variable name twice. It composes with a format spec, but the = must come before the colon: f"{x=:.2f}", not f"{x:.2f=}" (verified below -- the wrong order is a real runtime ValueError, not just unidiomatic).

python
x = 42
name = "world"
print(f"{x=}")
print(f"{name=}")
print(f"{x + 1=}")   # works on any expression, not just a bare variable
python
pi = 3.14159265
print(f"{pi:.2f}")        # 2 decimal places
print(f"{1234567:,}")     # thousands separator
print(f"{name:>10}|")     # right-align in a 10-char field
print(f"{name:<10}|")     # left-align
print(f"{name:^10}|")     # center

print(f"{pi=:.2f}")       # debug specifier + format spec, correct order
try:
    eval('f"{pi:.2f=}"')  # wrong order -- verified as a real error, not just a style nit
except ValueError as e:
    print("wrong order raises:", e)

Interview angle

A minor but telling fluency signal — candidates who reach for f"{x=}" instead of print("x:", x) or manually typing print(f"x: {x}") are usually keeping up with newer Python idioms generally, which is a cheap, low-stakes way to get a read on how current someone's day-to-day Python is.

In the industry

f"{x=}" is now the default idiom for throwaway debug prints in interactive development and notebooks specifically because it's faster to type and impossible to get out of sync with the variable name (unlike a manually-written f"x: {x}", which silently goes stale if you rename x and forget the label). It's considered fine for scratch/debug code but is typically stripped or replaced with proper logging calls before code ships, since debug prints in general aren't meant to survive code review.