When using the string format operator (see Table B.5), you may enhance or fine-tune the object display with the directives shown in
Format Symbol
Conversion
%c
Character (integer [ASCII value] or string of length 1)
%r[a]
String conversion via repr() prior to formatting
%s
String conversion via str() prior to formatting
%d / %i
Signed decimal integer
%u[b]
Unsigned decimal integer
%o[b]
(Unsigned) octal integer
%x[b] / %X[b]
(Unsigned) hexadecimal integer (lower/UPPERcase letters)
%e / %E
Exponential notation (with lowercase 'e'/UPPERcase 'E')
%f / %F
Floating point real number (fraction truncates naturally)
%g / %G
The shorter of %e and %f/%E% and %F%
%%
Percent character ( % ) unescaped
[a] New in Python 2.0; likely unique only to Python.
[b] %u/%o/%x/%X of negative int will return a signed string in Python 2.4+.
Symbol
Functionality
*
Argument specifies width or precision
-
Use left justification
+
Use a plus sign ( + ) for positive numbers
<sp>
Use space-padding for positive numbers
#
Add the octal leading zero ('0') or hexadecimal leading '0x'
or '0X', depending on whether 'x' or 'X' were used
0
Use zero-padding (instead of spaces) when formatting numbers
%
'%%' leaves you with a single literal '%'
(var)
Mapping variable (dictionary arguments)
m.n
m is the minimum total width and n is the number of digits to display after the decimal point (if applicable)