Home | History | Annotate | Download | only in doc
      1 .. _syntax:
      2 
      3 ********************
      4 Format String Syntax
      5 ********************
      6 
      7 Formatting functions such as :ref:`fmt::format() <format>` and :ref:`fmt::print() <print>`
      8 use the same format string syntax described in this section.
      9 
     10 Format strings contain "replacement fields" surrounded by curly braces ``{}``.
     11 Anything that is not contained in braces is considered literal text, which is
     12 copied unchanged to the output.  If you need to include a brace character in the
     13 literal text, it can be escaped by doubling: ``{{`` and ``}}``.
     14 
     15 The grammar for a replacement field is as follows:
     16 
     17 .. productionlist:: sf
     18    replacement_field: "{" [`arg_id`] [":" `format_spec`] "}"
     19    arg_id: `integer` | `identifier`
     20    integer: `digit`+
     21    digit: "0"..."9"
     22    identifier: `id_start` `id_continue`*
     23    id_start: "a"..."z" | "A"..."Z" | "_"
     24    id_continue: `id_start` | `digit`
     25 
     26 In less formal terms, the replacement field can start with an *arg_id*
     27 that specifies the argument whose value is to be formatted and inserted into
     28 the output instead of the replacement field.
     29 The *arg_id* is optionally followed by a *format_spec*, which is preceded
     30 by a colon ``':'``.  These specify a non-default format for the replacement value.
     31 
     32 See also the :ref:`formatspec` section.
     33 
     34 If the numerical arg_ids in a format string are 0, 1, 2, ... in sequence,
     35 they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be
     36 automatically inserted in that order.
     37 
     38 Some simple format string examples::
     39 
     40    "First, thou shalt count to {0}" // References the first argument
     41    "Bring me a {}"                  // Implicitly references the first argument
     42    "From {} to {}"                  // Same as "From {0} to {1}"
     43 
     44 The *format_spec* field contains a specification of how the value should be
     45 presented, including such details as field width, alignment, padding, decimal
     46 precision and so on.  Each value type can define its own "formatting
     47 mini-language" or interpretation of the *format_spec*.
     48 
     49 Most built-in types support a common formatting mini-language, which is
     50 described in the next section.
     51 
     52 A *format_spec* field can also include nested replacement fields in certain
     53 positions within it. These nested replacement fields can contain only an
     54 argument id; format specifications are not allowed. This allows the
     55 formatting of a value to be dynamically specified.
     56 
     57 See the :ref:`formatexamples` section for some examples.
     58 
     59 .. _formatspec:
     60 
     61 Format Specification Mini-Language
     62 ==================================
     63 
     64 "Format specifications" are used within replacement fields contained within a
     65 format string to define how individual values are presented (see
     66 :ref:`syntax`).  Each formattable type may define how the format
     67 specification is to be interpreted.
     68 
     69 Most built-in types implement the following options for format specifications,
     70 although some of the formatting options are only supported by the numeric types.
     71 
     72 The general form of a *standard format specifier* is:
     73 
     74 .. productionlist:: sf
     75    format_spec: [[`fill`]`align`][`sign`]["#"]["0"][`width`]["." `precision`][`type`]
     76    fill: <a character other than '{' or '}'>
     77    align: "<" | ">" | "=" | "^"
     78    sign: "+" | "-" | " "
     79    width: `integer` | "{" `arg_id` "}"
     80    precision: `integer` | "{" `arg_id` "}"
     81    type: `int_type` | "a" | "A" | "c" | "e" | "E" | "f" | "F" | "g" | "G" | "p" | "s"
     82    int_type: "b" | "B" | "d" | "n" | "o" | "x" | "X"
     83 
     84 The *fill* character can be any character other than '{' or '}'.  The presence
     85 of a fill character is signaled by the character following it, which must be
     86 one of the alignment options.  If the second character of *format_spec* is not
     87 a valid alignment option, then it is assumed that both the fill character and
     88 the alignment option are absent.
     89 
     90 The meaning of the various alignment options is as follows:
     91 
     92 +---------+----------------------------------------------------------+
     93 | Option  | Meaning                                                  |
     94 +=========+==========================================================+
     95 | ``'<'`` | Forces the field to be left-aligned within the available |
     96 |         | space (this is the default for most objects).            |
     97 +---------+----------------------------------------------------------+
     98 | ``'>'`` | Forces the field to be right-aligned within the          |
     99 |         | available space (this is the default for numbers).       |
    100 +---------+----------------------------------------------------------+
    101 | ``'='`` | Forces the padding to be placed after the sign (if any)  |
    102 |         | but before the digits.  This is used for printing fields |
    103 |         | in the form '+000000120'. This alignment option is only  |
    104 |         | valid for numeric types.                                 |
    105 +---------+----------------------------------------------------------+
    106 | ``'^'`` | Forces the field to be centered within the available     |
    107 |         | space.                                                   |
    108 +---------+----------------------------------------------------------+
    109 
    110 Note that unless a minimum field width is defined, the field width will always
    111 be the same size as the data to fill it, so that the alignment option has no
    112 meaning in this case.
    113 
    114 The *sign* option is only valid for number types, and can be one of the
    115 following:
    116 
    117 +---------+----------------------------------------------------------+
    118 | Option  | Meaning                                                  |
    119 +=========+==========================================================+
    120 | ``'+'`` | indicates that a sign should be used for both            |
    121 |         | positive as well as negative numbers.                    |
    122 +---------+----------------------------------------------------------+
    123 | ``'-'`` | indicates that a sign should be used only for negative   |
    124 |         | numbers (this is the default behavior).                  |
    125 +---------+----------------------------------------------------------+
    126 | space   | indicates that a leading space should be used on         |
    127 |         | positive numbers, and a minus sign on negative numbers.  |
    128 +---------+----------------------------------------------------------+
    129 
    130 The ``'#'`` option causes the "alternate form" to be used for the
    131 conversion.  The alternate form is defined differently for different
    132 types.  This option is only valid for integer and floating-point types.
    133 For integers, when binary, octal, or hexadecimal output is used, this
    134 option adds the prefix respective ``"0b"`` (``"0B"``), ``"0"``, or
    135 ``"0x"`` (``"0X"``) to the output value.  Whether the prefix is
    136 lower-case or upper-case is determined by the case of the type
    137 specifier, for example, the prefix ``"0x"`` is used for the type ``'x'``
    138 and ``"0X"`` is used for ``'X'``.  For floating-point numbers the
    139 alternate form causes the result of the conversion to always contain a
    140 decimal-point character, even if no digits follow it. Normally, a
    141 decimal-point character appears in the result of these conversions
    142 only if a digit follows it. In addition, for ``'g'`` and ``'G'``
    143 conversions, trailing zeros are not removed from the result.
    144 
    145 .. ifconfig:: False
    146 
    147    The ``','`` option signals the use of a comma for a thousands separator.
    148    For a locale aware separator, use the ``'n'`` integer presentation type
    149    instead.
    150 
    151 *width* is a decimal integer defining the minimum field width.  If not
    152 specified, then the field width will be determined by the content.
    153 
    154 Preceding the *width* field by a zero (``'0'``) character enables
    155 sign-aware zero-padding for numeric types.  This is equivalent to a *fill*
    156 character of ``'0'`` with an *alignment* type of ``'='``.
    157 
    158 The *precision* is a decimal number indicating how many digits should be
    159 displayed after the decimal point for a floating-point value formatted with
    160 ``'f'`` and ``'F'``, or before and after the decimal point for a floating-point
    161 value formatted with ``'g'`` or ``'G'``.  For non-number types the field
    162 indicates the maximum field size - in other words, how many characters will be
    163 used from the field content. The *precision* is not allowed for integer,
    164 character, Boolean, and pointer values.
    165 
    166 Finally, the *type* determines how the data should be presented.
    167 
    168 The available string presentation types are:
    169 
    170 +---------+----------------------------------------------------------+
    171 | Type    | Meaning                                                  |
    172 +=========+==========================================================+
    173 | ``'s'`` | String format. This is the default type for strings and  |
    174 |         | may be omitted.                                          |
    175 +---------+----------------------------------------------------------+
    176 | none    | The same as ``'s'``.                                     |
    177 +---------+----------------------------------------------------------+
    178 
    179 The available character presentation types are:
    180 
    181 +---------+----------------------------------------------------------+
    182 | Type    | Meaning                                                  |
    183 +=========+==========================================================+
    184 | ``'c'`` | Character format. This is the default type for           |
    185 |         | characters and may be omitted.                           |
    186 +---------+----------------------------------------------------------+
    187 | none    | The same as ``'c'``.                                     |
    188 +---------+----------------------------------------------------------+
    189 
    190 The available integer presentation types are:
    191 
    192 +---------+----------------------------------------------------------+
    193 | Type    | Meaning                                                  |
    194 +=========+==========================================================+
    195 | ``'b'`` | Binary format. Outputs the number in base 2. Using the   |
    196 |         | ``'#'`` option with this type adds the prefix ``"0b"``   |
    197 |         | to the output value.                                     |
    198 +---------+----------------------------------------------------------+
    199 | ``'B'`` | Binary format. Outputs the number in base 2. Using the   |
    200 |         | ``'#'`` option with this type adds the prefix ``"0B"``   |
    201 |         | to the output value.                                     |
    202 +---------+----------------------------------------------------------+
    203 | ``'d'`` | Decimal integer. Outputs the number in base 10.          |
    204 +---------+----------------------------------------------------------+
    205 | ``'o'`` | Octal format. Outputs the number in base 8.              |
    206 +---------+----------------------------------------------------------+
    207 | ``'x'`` | Hex format. Outputs the number in base 16, using         |
    208 |         | lower-case letters for the digits above 9. Using the     |
    209 |         | ``'#'`` option with this type adds the prefix ``"0x"``   |
    210 |         | to the output value.                                     |
    211 +---------+----------------------------------------------------------+
    212 | ``'X'`` | Hex format. Outputs the number in base 16, using         |
    213 |         | upper-case letters for the digits above 9. Using the     |
    214 |         | ``'#'`` option with this type adds the prefix ``"0X"``   |
    215 |         | to the output value.                                     |
    216 +---------+----------------------------------------------------------+
    217 | ``'n'`` | Number. This is the same as ``'d'``, except that it uses |
    218 |         | the current locale setting to insert the appropriate     |
    219 |         | number separator characters.                             |
    220 +---------+----------------------------------------------------------+
    221 | none    | The same as ``'d'``.                                     |
    222 +---------+----------------------------------------------------------+
    223 
    224 Integer presentation types can also be used with character and Boolean values.
    225 Boolean values are formatted using textual representation, either ``true`` or
    226 ``false``, if the presentation type is not specified.
    227 
    228 The available presentation types for floating-point values are:
    229 
    230 +---------+----------------------------------------------------------+
    231 | Type    | Meaning                                                  |
    232 +=========+==========================================================+
    233 | ``'a'`` | Hexadecimal floating point format. Prints the number in  |
    234 |         | base 16 with prefix ``"0x"`` and lower-case letters for  |
    235 |         | digits above 9. Uses ``'p'`` to indicate the exponent.   |
    236 +---------+----------------------------------------------------------+
    237 | ``'A'`` | Same as ``'a'`` except it uses upper-case letters for    |
    238 |         | the prefix, digits above 9 and to indicate the exponent. |
    239 +---------+----------------------------------------------------------+
    240 | ``'e'`` | Exponent notation. Prints the number in scientific       |
    241 |         | notation using the letter 'e' to indicate the exponent.  |
    242 +---------+----------------------------------------------------------+
    243 | ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an     |
    244 |         | upper-case 'E' as the separator character.               |
    245 +---------+----------------------------------------------------------+
    246 | ``'f'`` | Fixed point. Displays the number as a fixed-point        |
    247 |         | number.                                                  |
    248 +---------+----------------------------------------------------------+
    249 | ``'F'`` | Fixed point. Same as ``'f'``, but converts ``nan`` to    |
    250 |         | ``NAN`` and ``inf`` to ``INF``.                          |
    251 +---------+----------------------------------------------------------+
    252 | ``'g'`` | General format.  For a given precision ``p >= 1``,       |
    253 |         | this rounds the number to ``p`` significant digits and   |
    254 |         | then formats the result in either fixed-point format     |
    255 |         | or in scientific notation, depending on its magnitude.   |
    256 |         |                                                          |
    257 |         | A precision of ``0`` is treated as equivalent to a       |
    258 |         | precision of ``1``.                                      |
    259 +---------+----------------------------------------------------------+
    260 | ``'G'`` | General format. Same as ``'g'`` except switches to       |
    261 |         | ``'E'`` if the number gets too large. The                |
    262 |         | representations of infinity and NaN are uppercased, too. |
    263 +---------+----------------------------------------------------------+
    264 | none    | The same as ``'g'``.                                     |
    265 +---------+----------------------------------------------------------+
    266 
    267 Floating-point formatting is locale-dependent.
    268 
    269 .. ifconfig:: False
    270 
    271    +---------+----------------------------------------------------------+
    272    |         | The precise rules are as follows: suppose that the       |
    273    |         | result formatted with presentation type ``'e'`` and      |
    274    |         | precision ``p-1`` would have exponent ``exp``.  Then     |
    275    |         | if ``-4 <= exp < p``, the number is formatted            |
    276    |         | with presentation type ``'f'`` and precision             |
    277    |         | ``p-1-exp``.  Otherwise, the number is formatted         |
    278    |         | with presentation type ``'e'`` and precision ``p-1``.    |
    279    |         | In both cases insignificant trailing zeros are removed   |
    280    |         | from the significand, and the decimal point is also      |
    281    |         | removed if there are no remaining digits following it.   |
    282    |         |                                                          |
    283    |         | Positive and negative infinity, positive and negative    |
    284    |         | zero, and nans, are formatted as ``inf``, ``-inf``,      |
    285    |         | ``0``, ``-0`` and ``nan`` respectively, regardless of    |
    286    |         | the precision.                                           |
    287    |         |                                                          |
    288    +---------+----------------------------------------------------------+
    289 
    290 The available presentation types for pointers are:
    291 
    292 +---------+----------------------------------------------------------+
    293 | Type    | Meaning                                                  |
    294 +=========+==========================================================+
    295 | ``'p'`` | Pointer format. This is the default type for             |
    296 |         | pointers and may be omitted.                             |
    297 +---------+----------------------------------------------------------+
    298 | none    | The same as ``'p'``.                                     |
    299 +---------+----------------------------------------------------------+
    300 
    301 .. _formatexamples:
    302 
    303 Format examples
    304 ===============
    305 
    306 This section contains examples of the format syntax and comparison with
    307 the printf formatting.
    308 
    309 In most of the cases the syntax is similar to the printf formatting, with the
    310 addition of the ``{}`` and with ``:`` used instead of ``%``.
    311 For example, ``"%03.2f"`` can be translated to ``"{:03.2f}"``.
    312 
    313 The new format syntax also supports new and different options, shown in the
    314 following examples.
    315 
    316 Accessing arguments by position::
    317 
    318    format("{0}, {1}, {2}", 'a', 'b', 'c');
    319    // Result: "a, b, c"
    320    format("{}, {}, {}", 'a', 'b', 'c');
    321    // Result: "a, b, c"
    322    format("{2}, {1}, {0}", 'a', 'b', 'c');
    323    // Result: "c, b, a"
    324    format("{0}{1}{0}", "abra", "cad");  // arguments' indices can be repeated
    325    // Result: "abracadabra"
    326 
    327 Aligning the text and specifying a width::
    328 
    329    format("{:<30}", "left aligned");
    330    // Result: "left aligned                  "
    331    format("{:>30}", "right aligned");
    332    // Result: "                 right aligned"
    333    format("{:^30}", "centered");
    334    // Result: "           centered           "
    335    format("{:*^30}", "centered");  // use '*' as a fill char
    336    // Result: "***********centered***********"
    337 
    338 Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign::
    339 
    340    format("{:+f}; {:+f}", 3.14, -3.14);  // show it always
    341    // Result: "+3.140000; -3.140000"
    342    format("{: f}; {: f}", 3.14, -3.14);  // show a space for positive numbers
    343    // Result: " 3.140000; -3.140000"
    344    format("{:-f}; {:-f}", 3.14, -3.14);  // show only the minus -- same as '{:f}; {:f}'
    345    // Result: "3.140000; -3.140000"
    346 
    347 Replacing ``%x`` and ``%o`` and converting the value to different bases::
    348 
    349    format("int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
    350    // Result: "int: 42;  hex: 2a;  oct: 52; bin: 101010"
    351    // with 0x or 0 or 0b as prefix:
    352    format("int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}", 42);
    353    // Result: "int: 42;  hex: 0x2a;  oct: 052;  bin: 0b101010"
    354 
    355 .. ifconfig:: False
    356 
    357    Using the comma as a thousands separator::
    358 
    359       format("{:,}", 1234567890);
    360       '1,234,567,890'
    361 
    362    Expressing a percentage::
    363 
    364       >>> points = 19
    365       >>> total = 22
    366       Format("Correct answers: {:.2%}") << points/total)
    367       'Correct answers: 86.36%'
    368 
    369    Using type-specific formatting::
    370 
    371       >>> import datetime
    372       >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
    373       Format("{:%Y-%m-%d %H:%M:%S}") << d)
    374       '2010-07-04 12:15:58'
    375 
    376    Nesting arguments and more complex examples::
    377 
    378       >>> for align, text in zip('<^>', ['left', 'center', 'right']):
    379       ...     '{0:{fill}{align}16}") << text, fill=align, align=align)
    380       ...
    381       'left<<<<<<<<<<<<'
    382       '^^^^^center^^^^^'
    383       '>>>>>>>>>>>right'
    384       >>>
    385       >>> octets = [192, 168, 0, 1]
    386       Format("{:02X}{:02X}{:02X}{:02X}") << *octets)
    387       'C0A80001'
    388       >>> int(_, 16)
    389       3232235521
    390       >>>
    391       >>> width = 5
    392       >>> for num in range(5,12):
    393       ...     for base in 'dXob':
    394       ...         print('{0:{width}{base}}") << num, base=base, width=width), end=' ')
    395       ...     print()
    396       ...
    397           5     5     5   101
    398           6     6     6   110
    399           7     7     7   111
    400           8     8    10  1000
    401           9     9    11  1001
    402          10     A    12  1010
    403          11     B    13  1011
    404 
    405