Draft for Information Only
Content
printf-style String Formatting Conversion Specifier Source and Reference
printf-style String Formatting
Note
The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). If the value being printed may be a tuple or dictionary, wrap it in a tuple.
Bytes objects (bytes/bytearray) have one unique built-in operation: the % operator (modulo). This is also known as the bytes formatting or interpolation operator. Given format % values (where format is a bytes object), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to using the sprintf() in the C language.
If format requires a single argument, values may be a single non-tuple object. 5 Otherwise, values must be a tuple with exactly the number of items specified by the format bytes object, or a single mapping object (for example, a dictionary).
Conversion Specifier
A conversion specifier contains two or more characters and has the following components, which must occur in this order:
%to mark the start of the specifier.
(Mapping key)(optional), consisting of a parenthesised sequence of characters (for example, (somename)).
Conversion flagsThe conversion flag characters are:(optional), which affect the result of some conversion types.FlagMeaning
#The value conversion will use the “alternate form” (where defined below).
0The conversion will be zero padded for numeric values.
-The converted value is left adjusted (overrides the '0' conversion if both are given).
A blank should be left before a positive number (or empty string) produced by a signed conversion.
+A sign character ('+' or '-') will precede the conversion (overrides a “space” flag).
Minimum field width(optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.
Precision(optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual precision is read from the next element of the tuple in values, and the value to convert comes after the precision.
Length modifier(optional) A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python – so e.g. %ld is identical to %d..
Conversion typeThe conversion types are:
TypeDescription
dSigned integer decimal.
iSigned integer decimal.
oSigned octal value.
uObsolete type – it is identical to 'd'.
xSigned hexadecimal (lowercase).
XSigned hexadecimal (uppercase).
eFloating point exponential format (lowercase).
EFloating point exponential format (uppercase).
fFloating point decimal format.
FFloating point decimal format.
gFloating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
GFloating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.
cSingle byte (accepts integer or single byte objects).
bBytes (any object that follows the buffer protocol or has __bytes__()).
s's' is an alias for 'b' and should only be used for Python2/3 code bases.
aBytes (converts any Python object using repr(obj).encode('ascii','backslashreplace)).
r'r' is an alias for 'a' and should only be used for Python2/3 code bases.
%No argument is converted, results in a '%' character in the result.
Notes:
The alternate form causes a leading octal specifier ('0o') to be inserted before the first digit.
The alternate form causes a leading '0x' or '0X' (depending on whether the 'x' or 'X' format was used) to be inserted before the first digit.
The alternate form causes the result to always contain a decimal point, even if no digits follow it.
The precision determines the number of digits after the decimal point and defaults to 6.
The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.
The precision determines the number of significant digits before and after the decimal point and defaults to 6.
If precision is N, the output is truncated to N characters.
b'%s' is deprecated, but will not be removed during the 3.x series.
b'%r' is deprecated, but will not be removed during the 3.x series.
See PEP 237.
Note
The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.
See also
PEP 461 - Adding % formatting to bytes and bytearray
New in version 3.5.
When the right argument is a dictionary (or other mapping type), then the formats in the bytes object must include a parenthesised mapping key into that dictionary inserted immediately after the '%' character. The mapping key selects the value to be formatted from the mapping. For example:
>>>
>>> print(b'%(language)s has %(number)03d quote types.' %
... {b'language': b"Python", b"number": 2})
b'Python has 002 quote types.'
In this case no * specifiers may occur in a format (since they require a sequential parameter list).
Source and Reference
©sideway
ID: 210100019 Last Updated: 1/19/2021 Revision: 0
|
|