This is a draft cheat sheet. It is a work in progress and is not finished yet.
Basic
{ [field_name] [! conversion] [: format_spec] }
- field_name
: variable name
- conversion
: pre-convert the variable
- format_spec
: format the output |
Examples
'{:-<10}'.format('abc')
|
|
'{:,.2f}'.format(12345.6789)
|
|
Type
f / F |
Floating point |
d |
Integer (decimal - default for int) |
% |
Percentage |
b |
Binary |
o |
Octal |
x / X |
Hex |
e / E |
Exponential |
g / G |
General (default for float) |
n |
number (generic) |
c |
character (unicode) |
|
|
Format Specification
: [align][sign][width][options][.precision][type]
|
Align
fill |
Any character to fill any extra space (precedes the alignment character) |
< |
left-aligned (default for objects) |
> |
right-aligned (default for numbers) |
^ |
centered |
= |
fill space between sign and number |
Sign
+ |
both positive and negative sign are shown |
- |
only negative sign is shown |
" " |
space for positive, "-" for negative |
Options
# |
Alternate form (*before width) |
, |
Adds "," as thousands separator |
_ |
Adds "_" as thousands separator |
|