printf style formatting in python

·

3 min read

String 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), % 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 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 precision 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.

The conversion types are:

'd'->Signed integer decimal.

'i'->Signed integer decimal.

'o'->Signed octal value.

'u'->Obsolete type – it is identical to 'd'.

'x'->Signed hexadecimal (lowercase).

'X'->Signed 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 lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise.

'G'->Floating point format. Uses uppercase exponential format if exponent is less than -4 or not 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()).

'a'->String (converts any Python object using ascii()).

'%'->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.