f-string
Python f-string formatting
All examples below were executed in Python interactive shell
- precision
>>> import math
>>> math.pi
3.141592653589793
>>> f'{math.pi:.2f}'
'3.14'
- grouping_option
>>> f'{1000000:,.2f}'
'1,000,000.00'
>>> f'{1000000:_.2f}'
'1_000_000.00'
- with numbers
>>> f'{12345:0>10}'
'0000012345'
- negative numbers
>>> f'{-12345:0=10}'
'-000012345'
- shortcut (no align)
>>> f'{12345:010}'
'0000012345'
>>> f'{-12345:010}'
'-000012345'
- sign (+/-)
>>> f'{12345:+}'
'+12345'
>>> f'{-12345:+}'
'-12345'
>>> f'{-12345:+10}'
' -12345'
>>> f'{-12345:+010}'
'-000012345'
- binary
>>> f'{10:b}'
'1010'
- octal
>>> f'{10:o}'
'12'
- hexadecimal
>>> f'{200:x}'
'c8'
>>> f'{200:X}'
'C8'
- scientific notation
>>> f'{345600000000:e}'
'3.456000e+11'
- character type
>>> f'{65:c}'
'A'
- with notation (base)
>>> f'{10:#b}'
'0b1010'
>>> f'{10:#o}'
'0o12'
>>> f'{10:#x}'
'0xa'
- percentage (multiply by 100)
>>> f'{0.25:0%}'
'25.000000%'
>>> f'{0.25:.0%}'
'25%'
License
To the extent possible under law, myshell-src has waived all copyright and related or neighboring rights to this file.