Show Menu

python string formatting Cheat Sheet by

python string formatting with call to .format()
development     python     string     format

Field defini­tions

replacement_field
"{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name
arg_name ("." attribute_name | "[" element_index "]")*
arg_name
[iden­tifier | digit+]
attribute_name
ident­ifier
element_index
digit+ | index_­string
index_­string
<any source character except "]"> +
conversion
"r" | "­s" | "­a"
format­_spec
Format Specification Mini-Language

field_name

The repla­cem­ent­_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_n­ame. 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_n­ames 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 asci­i()
The conve­rsion field forces a type conversion before format­ting, so not by the __fo­rma­t__() 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.

preci­sion 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 preci­sion is 6
E
Exponent notation. Same as e but with uppercase E
f
Fixed-­point notation. Default preci­sion is 6
F
Fixed-­point notation. Same as f but converts nan to NAN and inf to INF
g
General format. If preci­sion 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

Download the python string formatting Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/mutanclan_python-string-formatting.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Essential Python Cheat Sheet

          More Cheat Sheets by mutanclan

          tmux shortcuts Cheat Sheet
          vim tpope/vim-surround Cheat Sheet
          vim vixen firefox addon Cheat Sheet