String and Unicode objects have one unique built-in operation: the
% operator (modulo). Given format %
values (where format is a string or Unicode object),
% conversion specifications in format are replaced with
zero or more elements of values. The effect is similar to the
using sprintf() in the C language. If format is a
Unicode object, or if any of the objects being converted using the
%s conversion are Unicode objects, the result will be a
Unicode object as well.
If format requires a single argument, values may be a single non-tuple object. 2.5 Otherwise, values must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary).
A conversion specifier contains two or more characters and has the following components, which must occur in this order:
(somename)).
If the right argument is a dictionary (or any kind of mapping), then the formats in the string must have a parenthesized key into that dictionary inserted immediately after the "%" character, and each format formats the corresponding entry from the mapping. For example:
>>> count = 2 >>> language = 'Python' >>> print '%(language)s has %(count)03d quote types.' % vars() Python has 002 quote types.
In this case no * specifiers may occur in a format (since they
require a sequential parameter list).
The conversion flag characters are:
| Flag | Meaning |
|---|---|
| The value conversion will use the ``alternate form'' (where defined below). | |
| The conversion will be zero padded. | |
| The converted value is left adjusted (overrides "-"). | |
| (a space) 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). |
The length modifier may be h, l, and L may be
present, but are ignored as they are not necessary for Python.
The conversion types are:
| Conversion | Meaning |
|---|---|
| Signed integer decimal. | |
| Signed integer decimal. | |
| Unsigned octal. | |
| Unsigned decimal. | |
| Unsigned hexidecimal (lowercase). | |
| Unsigned hexidecimal (uppercase). | |
| Floating point exponential format (lowercase). | |
| Floating point exponential format (uppercase). | |
| Floating point decimal format. | |
| Floating point decimal format. | |
| Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise. | |
| Same as "E" if exponent is greater than -4 or less than precision, "F" otherwise. | |
| Single character (accepts integer or single character string). | |
| String (converts any python object using repr()). | |
| String (converts any python object using str()). | |
No argument is converted, results in a "%"
character in the result. (The complete specification is
%%.) |
Since Python strings have an explicit length, %s conversions
do not assume that '\0' is the end of the string.
For safety reasons, floating point precisions are clipped to 50;
%f conversions for numbers whose absolute value is over 1e25
are replaced by %g conversions.2.6 All other errors raise exceptions.
Additional string operations are defined in standard module string and in built-in module re.