1. Computing

String Formatting Operations

By

In Python, there is only one built-in string formatting operator: the modulo sign ('%'). This is also known as the string formatting or interpolation operator. Given a % value (where the value referenced is either a string or Unicode object), the % value is replaced with zero or more elements of values. If format is a Unicode object, or if any of the objects being converted using the "%s" conversion are Unicode objects, the result of the substitution will be a Unicode object, as well.

If the string format requires a single argument, values may be a single non-tuple object. Otherwise, values must be a tuple with exactly the number of items specified by the format string, or an object which maps to a single object (e.g., a dictionary value).

The string place holder contains two or more characters. The following components must occur in this order:

  1. The % character, which marks the start of the specifier.
  2. Mapping key consisting of a parenthesised sequence of characters (for example, (somename)). This is optional.
  3. Conversion flags which affect the result of some conversion types. This, too, is optional.
  4. Minimum field width. 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. This is optional
  5. Precision, 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. This is optional.
  6. Length modifier. Again, this is optional.
  7. Conversion type.

When the right argument is a dictionary or other mapping type, the formats in the string must include a parenthesised mapping key into that dictionary. This should be inserted immediately after the % character. The mapping key selects the value to be formatted from the mapping.

The conversion flag characters are:

  • #: The value conversion will use the `alternate form'.
  • 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).

While a length modifier (h, l, or L) may be present, it is not necessary. If present, it is ignored.

The conversion types are:

  • d: Signed integer decimal.
  • i: Signed integer decimal.
  • o: Unsigned octal. (1)
  • u: Unsigned decimal.
  • x: Unsigned hexadecimal (lowercase).
  • X: Unsigned hexadecimal (uppercase).
  • e: Floating point exponential format (lowercase).
  • E: Floating point exponential format (uppercase).
  • f: Floating point decimal format.
  • F: Floating point decimal format.
  • g: Floating point format. Uses exponential format if the exponent is greater than -4 or less than precision, decimal format otherwise.
  • G: Floating point format. Uses exponential format if the exponent is greater than -4 or less than precision, decimal format otherwise.
  • c: Single character (accepts integer or single character string).
  • r: String (converts any Python object using repr()).
  • s: String (converts any Python object using str()).
  • %: No argument is converted, results in the appearance of '%' in the result.

Finally, a few idiosyncracies of Python strings should be noted. Since Python strings have an explicit length, the use of '%s' does not assume that '\0' (i.e., the null character) is the end of the string.

Floating point precisions are, for reasons of safety, clipped to 50 characters in length. When using floating point integers for very large or very small numbers, remember that '%f' conversions for numbers whose absolute value is over 1e25 are replaced by '%g' conversions. Exceptions are raised for all other errors.

  1. About.com
  2. Computing
  3. Python
  4. Python Library
  5. Python Modules - Built-In Functions - String Formatting Operations

©2014 About.com. All rights reserved.