| Python Library Reference |
String and Unicode objects have one unique built-in operation: the% operator (modulo). This is also known as the stringformatting orinterpolation operator. Givenformat %values (whereformat is a string orUnicode object),% conversion specifications informatare replaced with zero or more elements ofvalues. The effectis similar to the usingsprintf() in the C language. Ifformat is a Unicode object, or if any of the objects beingconverted using the%s conversion are Unicode objects, theresult will be a Unicode object as well.
Ifformat requires a single argument,values may be asingle non-tuple object.2.8 Otherwise,values must be a tuple withexactly the number of items specified by the format string, or asingle mapping object (for example, a dictionary).
A conversion specifier contains two or more characters and has thefollowing components, which must occur in this order:
(somename)).If the right argument is a dictionary (or any kind of mapping), thenthe formats in the stringmust have a parenthesized key intothat dictionary inserted immediately after the "%"character, and each format formats the corresponding entry from themapping. 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 theyrequire a sequential parameter list).
The conversion flag characters are:
| Flag | Meaning |
|---|---|
| # | The value conversion will use the ``alternate form'' (where defined below). |
| 0 | The conversion will be zero padded for numeric values. |
| - | The converted value is left adjusted (overrides the "0" conversion if both are given). |
| (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 beh,l, andL may bepresent, but are ignored as they are not necessary for Python.
The conversion types are:
| Conversion | Meaning | Notes |
|---|---|---|
| d | Signed integer decimal. | |
| i | Signed integer decimal. | |
| o | Unsigned octal. | (1) |
| u | Unsigned decimal. | |
| x | Unsigned hexidecimal (lowercase). | (2) |
| X | Unsigned hexidecimal (uppercase). | (2) |
| e | Floating point exponential format (lowercase). | |
| E | Floating point exponential format (uppercase). | |
| f | Floating point decimal format. | |
| F | Floating point decimal format. | |
| g | Same as "e" if exponent is greater than -4 or less than precision, "f" otherwise. | |
| G | Same as "E" if exponent is greater than -4 or less than precision, "F" otherwise. | |
| c | Single character (accepts integer or single character string). | |
| r | String (converts any python object usingrepr()). | (3) |
| s | String (converts any python object usingstr()). | (4) |
| % | No argument is converted, results in a "%" character in the result. |
Notes:
'0x' or'0X' (depending on whether the "x" or "X" format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.%r conversion was added in Python 2.0.Since Python strings have an explicit length,%s conversionsdo 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 1e25are replaced by%g conversions.2.9 All other errors raise exceptions.
Additional string operations are defined in standard modulesstring andre.
| Python Library Reference |