python string formatting Cheat Sheet

A Programming › Python cheat sheet in English by mutanclan

python string formatting with call to .format()

24,687 views 4,762 downloads 20 Apr 2019 History
3.7 avg · 0 ratings

Field defini­tions

replacement_field
"­{" [field­_name] ["!" conver­sion] [":" format­_spec] "­}"
field_name
arg_name ("." attrib­ute­_name | "­[" elemen­t_index "­]")*
arg_name
[ident­ifier | digit+]
attribute_name
identifier
element_index
digit+ | index_­string
index_­string
<any source character except "]"> +
conversion
"­r" | "­s" | "­a"
format­_spec
Format Specification Mini-Language

field_name

The replac­eme­nt_­field can start with a field_name to specify the object whose value is to be formatted and inserted.

The field_name begins with an arg_name. The arg_name can be followed by any number of index or attribute expres­sions.

arg_name

An arg_name is either a number or a keyword. If it's a number it refers to a positional argument. If it's a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0,1,2 in sequence, the can be omitted (They are automa­tically inserted).

attrib­ute­_name

An expression of the form '.name' selects the named attribute using getattr()

elemen­t_index

An expression of the form '[index]' does an index lookup using __getitem__().

For example:
List index: [0]
Dictionary: [name]

conversion

!s
calls str()
!r
calls repr()
!a
calls ascii()
The conversion field forces a type conversion before format­ting, so not by the __form­at__() method of the value itself.

String presen­tation types

s
String format. This is the default for strings
None
The same as s

Integer presen­tation types

b
Binary format. Outputs the number in base 2
c
Character. Converts the integer to unicode
d
Decimal integer. Outputs number in base 10
o
Octal format. Outputs number in base 8
x
Hex format. Outputs number in base 16 using lowercase letters
X
Hex format. Outputs number in base 16 using uppercase letters
n
Number. Same as d but uses current locale setting for the separator
None
Same as d

Format Specif­ication Mini-L­anguage

format_spec
[[fill]align][sign][#][0][width][grouping_option][.precision][type]
fill
<any charac­ter>
align
"­<" | "­>" | "­=" | "­^"
sign
"­+" | "­-" | " "
width
digit+
grouping_option
"­_" | "­,"
precision
digit+
type
"b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

fill, sign and align

<
Force left-a­lig­nment within available space
>
Force rigth-­ali­gnment within available space
=
Only valid for numeric types. Forces the padding to be placed after the sign but before the digits
^
Forces the field to be centered within available space
+
Use a sign for both positiv and negative numbers
-
Use sign only for negative numbers
space
Use a leading space for positiv numbers and a minus sign for negative numbers
#
Causes the alternate form to be used for the conver­sion. binary: 0b, octal: 0o and hex: 0x. For floats, complex and Decimal types that causes to contain a trailing decima­l-point even if no digits follow it
,
Use , for thousands separator
_
Use _ for thousands separator
If an align value is specified it can be preceded by a fill character, that can be any character (default is space)

The sign option is only valid on numeric types

width and precision

width is a decimal integer defining the minimum field width. A leading 0 enables sign-aware zero-p­adding for numeric types.

precision is a decimal number indicating how many digits should be displayed after the decimal point. For non-number types it indicates the maximum field-­size. Not allowed for integer values

Floating point and decimal presen­tation types

e
Exponent notation using the letter e to indicate the exponent. Default precision is 6
E
Exponent notation. Same as e but with uppercase E
f
Fixed-­point notation. Default precision is 6
F
Fixed-­point notation. Same as f but converts nan to NAN and inf to INF
g
General format. If precision is p>=1 this rounds the number to p signif­icant digits. Output format is either fixed-­point or in scientific notation, depending on the magnitude
G
General format. Same as g but switches to E if the number gets too large.
n
Number. Same as g but use current locale for the number separator character
%
Percen­tage. Multiplies number by 100 and displays it in f format followed by a percentage sign
None
Same as g but fixed-­point notation has at least one digit past the decimal point

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 3

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.