Skip to content

Instantly share code, notes, and snippets.

@ArthurDelannoyazerty
Last active July 1, 2025 16:29
Show Gist options
  • Save ArthurDelannoyazerty/591a4ee5e4028430a2a60973d002a5a0 to your computer and use it in GitHub Desktop.
Save ArthurDelannoyazerty/591a4ee5e4028430a2a60973d002a5a0 to your computer and use it in GitHub Desktop.
Python f strings examples

Center text

heading = "Centered string"
print( f'{heading:=^30}' )      #| = is for the char to print | ^ Is to center the text | 30 is the total length of the final string

'=======Centered string========'

Align text

id1 = '9'
id2 = '100'
t = 'abcd
f'{id1} | {t}'
f'{id2} | {t}'
'9 | abcd'
'100 | abcd'
f'{id1} | {t}'
f'{id2:4} | {t}'
'9    | abcd'
'100  | abcd'

Number separator

i = -1234567
print( f'{integer:,}' )

'-1,234,567'

Argment format

sep = '_'
print( f'{integer:{sep}}' )

'-1_234_567'

Float rounding

nb = 1234567.9876
print( f'{floating_point:,.2f} )

'1,234,567.99'

Object str() & repr()

jane = Person("Jane Doe", 25)
f'{jane!s}'
"I'm Jane Doe, and I'm 25 years old."
f'{jane!r}'
"Person(name='Jane Doe', age=25)"

Variable

variable = "Some mysterious value"
f"{variable = }"
variable = 'Some mysterious value'
f"{variable=}"
variable='Some mysterious value'
f"{variable= }"
variable= 'Some mysterious value'
f"{variable =}"
variable ='Some mysterious value'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment