String Formatting: printf-style
String objects have one unique built-in operation: the %
operator (modulo).
This is also known as the string formatting or interpolation operator
[doch]. Given format % values
(where
format is a string), %
conversion specifications in format are replaced
with zero or more elements of values. If format requires a single argument,
values may be a single non-tuple object.
The conversion flag characters are:
Flag |
Meaning |
---|---|
|
The value conversion will use the “alternative form” |
|
The conversion will be zero padded for numeric values |
|
The converted value is left adjusted (override the |
|
(a space) A blank should be left before a positive number |
|
A sign character ( |
The conversion types are:
Conversion |
Meaning |
---|---|
|
Signed integer decimal |
|
Signed integer decimal |
|
Signed octal value |
|
Obsolete type - it is identical to |
|
Signed hexadecimal (lowercase) |
|
Signed hexadecimal (uppercase) |
|
Floating point exponential format (lowercase) |
|
Floating point exponential format (uppercase) |
|
Floating point decimal format |
|
Floating point format. Uses lowercase exponential format if exponent is less that -4 or not less than precision, decimal format otherwise. |
|
Single character (accepts integer or single character string) |
|
Converts any Python object using |
|
Converts any Python object using |
|
Converts any Python object using |
|
No arguments is converted, results in a |