Show Menu
Cheatography

Python String Formatting Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

C-printf Style

General form
pf_string1 % ( arg1 [, arg2 ...] )
 
pf_string2 % { 'name1' : arg1[, ...} )
pf_string1
" <li­ter­al>­|<f­mt1> ... "
pf_string2
" <li­ter­al>­|<f­mt2> ... "
<li­ter­al>
any code point outside of fmt is output as is
%%
outputs '%' (escape)
<fm­t1>
% [<c­onv­ers­ion­-fl­ags­>] [<m­in-­fie­ld-­wid­th>] [<p­rec­isi­on>] [<l­eng­th>] <co­nve­rsi­on-­typ­e>
<fm­t2>
% "­(" <ke­y> "­)" [<c­onv­ers­ion­-fl­ags­>] [<m­in-­fie­ld-­wid­th>] [<p­rec­isi­on>] [<l­eng­th>] <co­nve­rsi­on-­typ­e>

printf­-style format specifiers

<ke­y>
mapping key-anme in passed dictionary
<co­nve­rsi­on-­fla­gs>
"­#"
conversion uses altern­ative form (see altern­ative form in format mini-l­anguage section)
 
"­0"
0 padded numeric value
 
"­-"
left adjust (overrides zero-p­adding)
 
" "
leave space before positive number
 
"­+"
sign character "­-" or "­+" will preceede the converted number
<mi­n-f­iel­d-w­idt­h>
"­*"
minimum width read from passed value tuple and must be followed by the value itself
 
<n>
minimum field width
<pr­eci­sio­n>
"." "­*"
precision read from next value in value tuple followed by th evalue itself
 
"." <n>
precision, <n> digits after decimal separator
<le­ngt­h-m­odi­fie­r>
"­h" | "­l" | "­L"
ignored in Python
<co­nve­rsi­on-­typ­e>
"­d"|"i­"
signed integer
 
"­o"
signed octal
 
"­u"
(obsolte) same as "­d"
 
"­x"
signed hexade­cimal (lower case)
 
"­X"
signed hexade­cimal (upper case)
 
"­e"
floating point number in expone­ntial form (lowercase 'e')
 
"­E"
floating point number in expone­ntial form (uppercase 'E')
 
"­f"|"F­"
decimal floating point
 
"­g"
floating point form,at using expone­ntial format iof exponent less than -4 (lowercase 'e')
 
"­G"
floating point form,at using expone­ntial format iof exponent less than -4 (uppercase 'e')
 
"­c"
single character
 
"­r"
string - converts any object using repr()
 
"­s"
string - converts any object using str()
 
"­a"
string - converts any object using ascii()
 
"­%"
literal '%'

String format

str.fo­rmat()
<fo­rma­t-s­tri­ng> "." format( <ar­gs> )
<format string>
literal string with <fi­eld> defini­tions
<ar­gs>
<ar­g1>, <ar­g2>, ...
poisit­ional arguments referenced by index
 
<kw­1> "­=" <ar­g1>; <kw­2> "­=" <ar­g2>, ...
keyword args referenced by name
 
"­**" dctVar
args as dictio­nary, referenced by name
<fi­eld>
"­{" [<f­iel­d_n­ame­>] ["!" <co­nve­rsi­on>] [":" <fo­rma­t_s­pec­>] "­}"
<fi­eld­_na­me>
<ar­g_n­ame> ("." <at­tri­but­e_n­ame> | "­[" <el­eme­nt_­ind­ex> "­]")*
<ar­g_n­ame>
[<d­igi­t>+ | <id­ent­ifi­er> ]
refering by index (posit­ional arguments) or name (passed named arguments or dict variable) (see <ar­gs>)
<at­tri­but­e_n­ame>
<id­ent­ifi­er>
<el­eme­nt_­ind­ex>
<di­git­>+ | <in­dex­_st­rin­g>
<in­dex­_st­rin­g>
<an­y-c­har­acter except "­]"> +
<co­nve­rsi­on>
"­r"
convert to string via repr()
 
"­s"
convert to string via str()
 
"­a"
convert to string via ascii()
<fo­rma­t-s­pec>
see format specif­ication mini-l­anguage
 

Formatted String (V3.6+)

"­f"|"F­" \" <literal>|"{{"|"}}"|<field> ... \"
<li­ter­al>
literal character
any code point except "­{", "­}" or NULL
"{{" or "­}}"
escape
outputs "­{" respec­tively "­}"

<fi­eld>

<fi­eld>
"­{" <fi­eld­def> "­}"
<fi­eld­def>
<ex­pr> [ "­!" <co­nve­rsi­on> ] [ "­:" <fo­rma­t> ]
<ex­pr>
( <co­ndi­tio­nal­_ex­pre­ssi­on> | "" <or­_ex­pr>) ("," <co­ndi­tio­nal­_ex­pre­ssi­on> | "­," "" <or­_ex­pr> )* [","] | <yi­eld­_ex­pre­ssi­on>
virtually any python expres­sions works here. Some minor restri­ctions apply.
<co­ndi­tio­nal­_ex­pr>
<or­_te­st> [ "­if" <or­_te­st> "­els­e" <ex­pre­ssi­on>
<ex­pre­ssi­on>
<co­ndi­tio­nal­_ex­pr> | <la­mbd­a>
<la­mbd­a>
python lambda expression
<or­_te­st>
boolean expression using "­or", "­and­", "­not­" and compar­isons
<or­_ex­pr>
bitwise logical and shift expression using "­|", "­&", "­^", "­<<" and "­>>"
<co­nve­rsi­on>
"­s"
converted to string via str()
 
"­r"
converted to string via repr()
 
"­a"
converted to string via ascii()
<fo­rma­t>
( <li­ter­al> | NULL | <fi­eld> ) *
format specifier compatible to str.fo­rmat() method (see format specif­ication mini-l­anguage below)

Format Specif­ication Mini-L­anguage

<fo­rma­t_s­pec>
[[<­fil­l>]­<al­ign­>] [<s­ign­>][­#][­0][­<wi­dth­>][­<gr­oup­ing­>][.<p­rec­isi­on>­][<­typ­e>]
<fi­ll>
<any charac­ter>
fill in the empty space in an aligned value
<al­ign>
"­<"
left-a­liogn
 
"­>"
right-­align
 
"­="
numeric pĆ¼adding takes place after the sign (like = padding)
 
"­^"
center
<si­gn>
"­+"
show sign for both positive and negative numbers
 
"­-"
show sign for negative nuimbers only (default)
 
<sp­ace>
show space for positive and '-' for negative numbers
"­#"
altern­ative form
(see section below)
"­0"
"­0" in front of the <wi­dth> field if no alignment is specified is wual to "­0=" fill and align character combin­ation
number padded by 0 between sign and digits
<wi­dth>
minimal field width
<gr­oup­ing>
"­,"
use comma for 1000 separator (V3.1+)
 
"­_"
use "­_" (under­score) for 1000 separator (V3.6+)
<pr­eci­sio­n>
number of digits after the decimal point
<ty­pe> (integer)
"­b"
binary in base-2
 
"­c"
character
 
"­d"
decimal integer
 
"­o"
octal integer (base-8)
 
"­x"
hex format (base 16) using lowercase letters
 
"­X"
hex format (base 16) using uppercase letters
 
"­n"
integer number (like "­d"), using current locale for number separators
 
None
same as "­d"
<ty­pe> (float)
"­e"
Exponent (scien­tific) notation using lowercase "­e". Default precision is 6
 
"­E"
Exponent (scien­tific) notation using uppercase "­E". Default precision is 6
 
"­f"
fixed-­point notation with default precision of 6
 
"­F"
Same as "­f" but "­nan­" and "­inf­" are displayed as "­NAN­" and "­INF­"
 
"­g"
general format with rounded result at given precision digit
 
"­G"
same as "­g" except that uppercase "­E", "­NAN­" and "­INF­" are displayed
 
"­n"
same as "­g" except that it uses current locale for number separator characters
 
"­%"
precen­tage, multiplies number by 100, displays it in fixed format (like "­f") and appends "­%"
 
None
similar to "­g" buit has at least 1 digit past the decimal point
<ty­pe> (stzring)
"­s"
string
 
None
same as "­s"

Altern­ative forms (#)

for octal
"­0o" preceeds octal number
for hexade­cimal
"­0x" preceeds hexade­cimal number
floating point
always contain decimal point even if no digit follows
if precision is given
output truncated to precision