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).
x = 42
name = "world"
print(f"{x=}")
print(f"{name=}")
print(f"{x + 1=}") # works on any expression, not just a bare variable
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)