python string formatting Cheat Sheet
by mutanclan (mutanclan) via [Link]/79625/cs/19406/
Field definitions conversion
replacement_field "{" [field_name] ["!" conversion] [":" format_spec] "}" !s calls str()
field_name arg_name ("." attribute_name | "[" element_index "]")* !r calls repr()
arg_name [identifier | digit+] !a calls ascii()
attribute_name identifier The conversion field forces a type
conversion before formatting, so not by the
element_index digit+ | index_string
__format__() method of the value itself.
index_string <any source character except "]"> +
conversion "r" | "s" | "a" String presentation types
format_spec Format Specification Mini-Language s String format. This is the default for
strings
field_name
None The same as s
The replacement_field can start with a field_
name to specify the object whose value is to be
formatted and inserted. Integer presentation types
The field_name begins with an arg_name. The arg_name can be followed by any number of
b Binary format. Outputs the number
index or attribute expressions.
in base 2
c Character. Converts the integer to
arg_name
unicode
An arg_name is either a number or a keyword. If it's a number it refers to a positional argument.
d Decimal integer. Outputs number in
If it's a keyword, it refers to a named keyword argument. If the numerical arg_names in a format
base 10
string are 0,1,2 in sequence, the can be omitted (They are automatically inserted).
o Octal format. Outputs number in
attribute_name base 8
x Hex format. Outputs number in
An expression of the form '.name' selects the named attribute using getattr()
base 16 using lowercase letters
element_index X Hex format. Outputs number in
base 16 using uppercase letters
An expression of the form '[index]' does an index lookup using __getitem__().
n Number. Same as d but uses
For example:
current locale setting for the
List index: [0]
separator
Dictionary: [name]
None Same as d
Format Specification Mini-Language
format_spec [[fill]align][sign]
[#][0][width]
[grouping_option]
[.precision][type]
fill <any character>
align "<" | ">" | "=" | "^"
sign "+" | "-" | " "
width digit+
By mutanclan (mutanclan) Published 20th April, 2019. Sponsored by [Link]
[Link]/mutanclan/ Last updated 20th April, 2019. Everyone has a novel in them. Finish
Page 1 of 2. Yours!
[Link]
python string formatting Cheat Sheet
by mutanclan (mutanclan) via [Link]/79625/cs/19406/
Format Specification Mini-Language (cont) width and precision
grouping_option "_" | "," width is a decimal integer defining the minimum field width. A leading
precision digit+ 0 enables sign-aware zero-padding for numeric types.
precision is a decimal number indicating how many digits should be
type "b" | "c" | "d" | "e" | "E" | "f" |
displayed after the decimal point. For non-number types it indicates
"F" | "g" | "G" | "n" | "o" | "s" |
the maximum field-size. Not allowed for integer values
"x" | "X" | "%"
Floating point and decimal presentation types
fill, sign and align
e Exponent notation using the letter e to indicate the exponent.
< Force left-alignment within available space
Default precision is 6
> Force rigth-alignment within available space
E Exponent notation. Same as e but with uppercase E
= Only valid for numeric types. Forces the padding to be
f Fixed-point notation. Default precision is 6
placed after the sign but before the digits
F Fixed-point notation. Same as f but converts nan to NAN
^ Forces the field to be centered within available space
and inf to INF
+ Use a sign for both positiv and negative numbers
g General format. If precision is p>=1 this rounds the number
- Use sign only for negative numbers to p significant digits. Output format is either fixed-point or in
space Use a leading space for positiv numbers and a minus sign scientific notation, depending on the magnitude
for negative numbers G General format. Same as g but switches to E if the number
# Causes the alternate form to be used for the conversion. gets too large.
binary: 0b, octal: 0o and hex: 0x. For floats, complex and n Number. Same as g but use current locale for the number
Decimal types that causes to contain a trailing decimal- separator character
point even if no digits follow it
% Percentage. Multiplies number by 100 and displays it in f
, Use , for thousands separator
format followed by a percentage sign
_ Use _ for thousands separator None Same as g but fixed-point notation has at least one digit past
If an align value is specified it can be preceded by a fill character, the decimal point
that can be any character (default is space)
The sign option is only valid on numeric types
By mutanclan (mutanclan) Published 20th April, 2019. Sponsored by [Link]
[Link]/mutanclan/ Last updated 20th April, 2019. Everyone has a novel in them. Finish
Page 2 of 2. Yours!
[Link]