0% found this document useful (0 votes)
3 views2 pages

Mutanclan Python String Formatting

This document is a cheat sheet for Python string formatting, detailing the syntax for replacement fields, field names, argument names, and conversion types. It also covers the Format Specification Mini-Language, including options for width, precision, alignment, and sign. The cheat sheet provides a comprehensive overview of formatting types for strings and numbers, making it a useful reference for Python developers.

Uploaded by

aksjat19
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Mutanclan Python String Formatting

This document is a cheat sheet for Python string formatting, detailing the syntax for replacement fields, field names, argument names, and conversion types. It also covers the Format Specification Mini-Language, including options for width, precision, alignment, and sign. The cheat sheet provides a comprehensive overview of formatting types for strings and numbers, making it a useful reference for Python developers.

Uploaded by

aksjat19
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

python string formatting Cheat Sheet

by mutanclan (mutanclan) via [Link]/79625/cs/19406/

Field defini​tions conversion

replacement_field "{" [field_name] ["!" conversion] [":" format_spec] "}" !s calls str()

field_name arg_name ("." attribute_name | "[" element_index "]")* !r calls repr()

arg_name [iden​tifier | digit+] !a calls asci​i()

attribute_name ident​ifier The conve​rsion field forces a type


conversion before format​ting, so not by the
element_index digit+ | index_​string
__fo​rma​t__() method of the value itself.
index_​string <any source character except "]"> +

conversion "r" | "​s" | "​a" String presen​tation types


format​_spec Format Specification Mini-Language s String format. This is the default for
strings
field_name
None The same as s
The repla​cem​ent​_field can start with a field_
​ name to specify the object whose value is to be
formatted and inserted. Integer presen​tation types
The field​_name begins with an arg_n​ame. The arg_name can be followed by any number of
b Binary format. Outputs the number
index or attribute expres​sions.
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_n​ames in a format
base 10
string are 0,1,2 in sequence, the can be omitted (They are automa​tically inserted).
o Octal format. Outputs number in

attrib​ute​_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

elemen​t_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
Dictio​nary: [name]
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+

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 Specif​ication Mini-L​anguage (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-p​adding for numeric types.


preci​sion 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 presen​tation types


fill, sign and align
e Exponent notation using the letter e to indicate the exponent.
< Force left-a​lig​nment within available space
Default preci​sion is 6
> Force rigth-​ali​gnment 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 preci​sion 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 preci​sion is p>=1 this rounds the number
- Use sign only for negative numbers to p signif​icant 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 conver​sion. 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 decima​l- separator character
point even if no digits follow it
% Percen​tage. 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]

You might also like