0% found this document useful (0 votes)
75 views

String Formatting Operations

The % operator is used for string formatting or interpolation in Python. It replaces % conversion specifications in a format string with values. If format requires multiple arguments, values must be a tuple containing the correct number of items. A conversion specification contains components like the '%' character, mapping key, flags, field width, precision, and conversion type like 'd' for integers or 's' for strings.

Uploaded by

jokotoni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

String Formatting Operations

The % operator is used for string formatting or interpolation in Python. It replaces % conversion specifications in a format string with values. If format requires multiple arguments, values must be a tuple containing the correct number of items. A conversion specification contains components like the '%' character, mapping key, flags, field width, precision, and conversion type like 'd' for integers or 's' for strings.

Uploaded by

jokotoni
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

5.6.2.

String Formatting Operations String and Unicode objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator. 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 also be a Unicode object. 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 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: 1. The '%' character, which marks the start of the specifier. 2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)). 3. Conversion flags (optional), which affect the result of some conversion types. 4. 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. 5. Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision. 6. Length modifier (optional). 7. Conversion type. When the right argument is a dictionary (or other mapping type), then the formats in the string 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 '%(language)s has %(number)03d quote types.' % \ ... {"language": "Python", "number": 2} 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 '#' '0' '-' ''

Meaning

The value conversion will use the alternate form (where defined below). 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). 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.

The conversion types are: Conversion 'd' 'i' 'o' 'u' 'x' 'X' 'e' 'E' 'f' 'F' 'g' 'G' 'c' 'r' 's' '%' Signed integer decimal. Signed integer decimal. Signed octal value. Obsolete type it is identical to 'd'. Signed hexadecimal (lowercase). Signed hexadecimal (uppercase).

Meaning

Notes

(1) (7) (2) (2) (3) (3) (3) (3)

Floating point exponential format (lowercase). Floating point exponential format (uppercase). Floating point decimal format. Floating point decimal format.

Floating point format. Uses lowercase exponential format if exponent is less than -4 or not (4) less than precision, decimal format otherwise. Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format 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. (5) (6) (4)

Notes: 1. The alternate form causes a leading zero ('0') 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. 2. The alternate form causes a leading '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. 3. 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. 4. 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. 5. The %r conversion was added in Python 2.0. The precision determines the maximal number of characters used. 6. If the object or format provided is a unicode string, the resulting string will also be unicode. The precision determines the maximal number of characters used. 7. See PEP 237. Since Python strings have an explicit length, %s conversions do not assume that '\0' is the end of the string. Changed in version 2.7: %f conversions for numbers whose absolute value is over 1e50 are no longer replaced by %g conversions. Additional string operations are defined in standard modules string and re.

You might also like