Skip to main content

Python f-strings cheat sheet

  • Posted

This is a collection of Python f-string (“formatted string”) examples that I find helpful but don’t always remember.

Numbers

Print a number with commas:

>>> f'{123456789:,}'
'123,456,789'

Print a number and add spaces to make it a fixed width:

>>> f'{123456:9}'
'   123456'

Print a fixed-width number with commas:

>>> f'{123456:9,}'
'  123,456'