Python Format Strings

Enjoy this cheat sheet at its fullest within Dash, the macOS documentation browser.

Field Width and Alignment

'hey {:10}'.format('hello')

Specify width (Aign left, fill with spaces)


Fill with zeroes

Output: 0000000002


Specify width, align to center

Output: *************text*************

Member and Element Access

'{0}, {1}, {2}'.format(1, 2, 3)

Access arguments by ordinal position

Output: 1, 2, 3

'{}, {}, {}'.format(1, 2, 3)

Implicit positional arguments (2.7 and above only)

Output: 1, 2, 3

'{value1}, {value2}, {value2}'.format(value1=1, value2=2, value3=3)

Access keyword arguments by name

Output: 1, 2, 2

'{[1]}'.format(['first', 'second', 'third'])

Access element by index

Output: second


Access element attribute

Output: <stdin>

'{[name]}'.format({'name': 'something'})

Access element by key

Output: something

Numerical Representation


Hexadecimal representation

Output: 64


Hexadecimal representation (uppercase letters)

Output: D9F


Hexadecimal representation (including the 0x)

Output: 0x64


Binary representation

Output: 1100100


Character representation

Output: d


Decimal representation (default)

Output: 100


With thousands separator

Output: 1,000,000


Octal representation

Output: 144


Like d, but uses locale information for separators

Output: 100


Exponent notation

Output: 1.000000e-10


Exponent notation (capital 'E')

Output: 1.000000E-10


Fixed point

Output: 0.214286


General format

Output: 0.214286



Output: 66.000000%



Output: 0.214



Calling repr on arguments

Output: 'string'


Calling str on arguments

Output: 1.53438987