Format specification

Many formatting operations use a format specifier. The exact way it is interpreted depends on the type being formatted, but they mostly follow a common standard. All the parts are optional, but have to be in the order listed below. If no options are provided the default output is typically the same as using str() on the argument.
See Format string syntax .

  1. Alignment

    The alignment character specifies how to align the output within the given width. It can optionally be preceded by a fill character that is used to pad out the width. It requires a width to be specified to be useful, otherwise the field will occupy a minimum width.

    OptionMeaning
    < The field should be left aligned within the available space. This is the default for most types.
    ^ The field should be centered within the available space.
    > The field should be right aligned within the available space.
    = For numeric fields only this places the padding between the sign (if any) and the number.
  2. Sign

    For numeric fields only this controls how the sign is displayed.

    OptionMeaning
    + The sign should be included for both positive and negative numbers.
    - The sign should be included only for negative numbers. This is the default behaviour
    space The sign should be displayed for negative numbers. For positive numbers the sign is replaced with a space. This is useful when tabulating lists of positive and negative numbers.
  3. Sign coercion

    For floating point numbers only. Floating point numbers distinguish between positive zero and negative zero as two different values. Adding a z makes negative zero values displayed as positive.

  4. Alternate form

    Only valid for numeric types. Add a # causes the alternate form to be used. For binary, octal, and hexadecimal types this causes the radix to be displayed, for example 0x before the number. For floating point types the alternate form always includes a decimal point.

  5. Width

    Width is a decimal number that gives the minimum field width including all prefixes, separators, and other formatting characters. If the width starts with a 0 then it is equivalent to specifying an alignment of 0= except for string types.

  6. Grouping option

    Adding a , causes a comma to be used as a thousands separator for numeric types.
    Note: This is not locale sensitive. Much of Europe uses . as the thousands separator and , as the decimal point. The n type offers a better alternative.

    Adding a _ causes an underscore to be used as a thousands separator for floating point and d types. For b, o, x, and X an underscore is added every four digits.

  7. Precision

    Floating point and string types only. Adding a . and a decimal number specifies the precision. See types below for interpretation.

  8. Type

    A single character indicating how to present the data. Interpretation is dependant on the type of the value being formatted.

Integer types

Type characterMeaning
b Binary format (base 2). The alternate format prefixes the result with 0b.
c Character. The unicode character with the integer code point is output.
d Decimal format (base 10). The default if no type character is given.
o Octal format (base 8). The alternate format prefixes the result with 0o.
n Number format (base 10). Uses locale specific thousands separator and decimal point.
x Hexadecimal format (base 16). Uses the characters 0..9 and a..f. The alternate format prefixes the result with 0x.
X Hexadecimal format (base 16). Uses the characters 0..9 and A..F. The alternate format prefixes the result with 0X.

Floating point types

These are used for floats and Decimal  types.

Type characterMeaning
e Scientific format: 1.234567e89.
Any precision specified is the number of digits after the decimal point. If no precision is given it defaults to six for floats and all required for Decimal. The alternate form always includes a decimal point.
E Scientific format as e but the exponent is indicated with an E.
f Fixed point: 123.456789.
Formats the number as a decimal. Any precision specified is the number of digits after the decimal point. If no precision is given it defaults to six for floats and all required for Decimal. The alternate form always includes a decimal point.
F Fixed point as f but nan (not-a-number) becomes NAN and inf (infinity) becomes INF
g General format.
This is the default. Formats the number using f unless it is too big where it switches to e. Any precision specified is the number of significant digits to round to. If no precision is given it defaults to six for floats. For precise details see the official documentation.
G General format as g but it switches to E and nan (not-a-number) becomes NAN and inf (infinity) becomes INF
n General format as g but using locale specific thousands separators and decimal points.
% Percentage format.
Multiplies the number by 100 then uses f and adds a %.

String types

Type characterMeaning
s String format. The precision is the maximum number of characters to be used. If the string is longer it will be truncated. This is the default and only format for strings and can be omitted.

Other types

Other types can interpret format specifiers as they see fit in their implementation of __format__(format). One notable example is datetime which has it's own format codes .