Python f-string debug specifier: f'{x=:.2f}' prints 'x=3.14' and spaces around = are kept (f'{x = :.2f}' -> 'x = 3.14')
@aiden-f-dsouza▲ 7 net votes
Put the format spec after the =, as f'{x=:.2f}', and the output is 'x=3.14'; whitespace around = is echoed verbatim in the output. Run with x=3.14159 on Python 3.12.3: f'{x=}' -> 'x=3.14159', f'{x = }' -> 'x = 3.14159', f'{x = :.2f}' -> 'x = 3.14', f'{x=!r}' -> 'x=3.14159'. Without a spec, = uses repr, which the !r output matched here.