@@ -325,11 +325,11 @@ The general form of a *standard format specifier* is:
325325 align: "<" | ">" | "=" | "^"
326326 sign: "+" | "-" | " "
327327 width_and_precision: [`width_with_grouping `][`precision_with_grouping `]
328- width_with_grouping: [`width `][`grouping_option `]
329- precision_with_grouping: "." [`precision `]`grouping_option`
328+ width_with_grouping: [`width `][`grouping `]
329+ precision_with_grouping: "." [`precision `][` grouping `]
330330 width: `~python-grammar:digit`+
331- grouping_option: "_" | ","
332331 precision: `~python-grammar:digit`+
332+ grouping: "," | "_"
333333 type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g"
334334 : | "G" | "n" | "o" | "s" | "x" | "X" | "%"
335335
@@ -385,13 +385,13 @@ following:
385385+---------+----------------------------------------------------------+
386386| Option | Meaning |
387387+=========+==========================================================+
388- | ``'+' `` | indicates that a sign should be used for both |
388+ | ``'+' `` | Indicates that a sign should be used for both |
389389| | positive as well as negative numbers. |
390390+---------+----------------------------------------------------------+
391- | ``'-' `` | indicates that a sign should be used only for negative |
391+ | ``'-' `` | Indicates that a sign should be used only for negative |
392392| | numbers (this is the default behavior). |
393393+---------+----------------------------------------------------------+
394- | space | indicates that a leading space should be used on |
394+ | space | Indicates that a leading space should be used on |
395395| | positive numbers, and a minus sign on negative numbers. |
396396+---------+----------------------------------------------------------+
397397
@@ -419,30 +419,7 @@ decimal-point character appears in the result of these conversions
419419only if a digit follows it. In addition, for ``'g' `` and ``'G' ``
420420conversions, trailing zeros are not removed from the result.
421421
422- .. index :: single: , (comma); in string formatting
423-
424- The ``',' `` option signals the use of a comma for a thousands separator for
425- floating-point presentation types and for integer presentation type ``'d' ``.
426- For other presentation types, this option is an error.
427- For a locale aware separator, use the ``'n' `` integer presentation type
428- instead.
429-
430- .. versionchanged :: 3.1
431- Added the ``',' `` option (see also :pep: `378 `).
432-
433- .. index :: single: _ (underscore); in string formatting
434-
435- The ``'_' `` option signals the use of an underscore for a thousands
436- separator for floating-point presentation types and for integer
437- presentation type ``'d' ``. For integer presentation types ``'b' ``,
438- ``'o' ``, ``'x' ``, and ``'X' ``, underscores will be inserted every 4
439- digits. For other presentation types, specifying this option is an
440- error.
441-
442- .. versionchanged :: 3.6
443- Added the ``'_' `` option (see also :pep: `515 `).
444-
445- *width * is a decimal integer defining the minimum total field width,
422+ The *width * is a decimal integer defining the minimum total field width,
446423including any prefixes, separators, and other formatting characters.
447424If not specified, then the field width will be determined by the content.
448425
@@ -463,12 +440,43 @@ indicates the maximum field size - in other words, how many characters will be
463440used from the field content. The *precision * is not allowed for integer
464441presentation types.
465442
466- The ``'_' `` or ``',' `` option after *precision * means the use of an underscore
467- or a comma for a thousands separator of the fractional part for floating-point
468- presentation types.
443+ The *grouping * option after *width * and *precision * fields specifies
444+ a digit group separator for the integral and fractional parts
445+ of a number respectively. It can be one of the following:
446+
447+ .. index ::
448+ single: , (comma); in string formatting
449+ single: _ (underscore); in string formatting
450+
451+ +---------+----------------------------------------------------------+
452+ | Option | Meaning |
453+ +=========+==========================================================+
454+ | ``',' `` | Inserts a comma every 3 digits for |
455+ | | integer presentation type ``'d' `` and |
456+ | | floating-point presentation types, excluding ``'n' ``. |
457+ | | For other presentation types, |
458+ | | this option is not supported. |
459+ +---------+----------------------------------------------------------+
460+ | ``'_' `` | Inserts an underscore every 3 digits for |
461+ | | integer presentation type ``'d' `` and |
462+ | | floating-point presentation types, excluding ``'n' ``. |
463+ | | For integer presentation types |
464+ | | ``'b' ``, ``'o' ``, ``'x' ``, and ``'X' ``, |
465+ | | underscores are inserted every 4 digits. |
466+ | | For other presentation types, |
467+ | | this option is not supported. |
468+ +---------+----------------------------------------------------------+
469+
470+ For a locale aware separator, use the ``'n' `` presentation type instead.
471+
472+ .. versionchanged :: 3.1
473+ Added the ``',' `` option (see also :pep: `378 `).
474+
475+ .. versionchanged :: 3.6
476+ Added the ``'_' `` option (see also :pep: `515 `).
469477
470478.. versionchanged :: 3.14
471- Support thousands separators for the fractional part.
479+ Support the * grouping * option for the fractional part.
472480
473481Finally, the *type * determines how the data should be presented.
474482
@@ -507,7 +515,7 @@ The available integer presentation types are:
507515 +---------+----------------------------------------------------------+
508516 | ``'n' `` | Number. This is the same as ``'d' ``, except that it uses |
509517 | | the current locale setting to insert the appropriate |
510- | | number separator characters. |
518+ | | digit group separators. |
511519 +---------+----------------------------------------------------------+
512520 | None | The same as ``'d' ``. |
513521 +---------+----------------------------------------------------------+
@@ -589,7 +597,8 @@ The available presentation types for :class:`float` and
589597 +---------+----------------------------------------------------------+
590598 | ``'n' `` | Number. This is the same as ``'g' ``, except that it uses |
591599 | | the current locale setting to insert the appropriate |
592- | | number separator characters. |
600+ | | digit group separators |
601+ | | for the integral part of a number. |
593602 +---------+----------------------------------------------------------+
594603 | ``'%' `` | Percentage. Multiplies the number by 100 and displays |
595604 | | in fixed (``'f' ``) format, followed by a percent sign. |
@@ -716,12 +725,16 @@ Replacing ``%x`` and ``%o`` and converting the value to different bases::
716725 >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
717726 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
718727
719- Using the comma or the underscore as a thousands separator::
728+ Using the comma or the underscore as a digit group separator::
720729
721730 >>> '{:,}'.format(1234567890)
722731 '1,234,567,890'
723732 >>> '{:_}'.format(1234567890)
724733 '1_234_567_890'
734+ >>> '{:_b}'.format(1234567890)
735+ '100_1001_1001_0110_0000_0010_1101_0010'
736+ >>> '{:_x}'.format(1234567890)
737+ '4996_02d2'
725738 >>> '{:_}'.format(123456789.123456789)
726739 '123_456_789.12345679'
727740 >>> '{:._}'.format(123456789.123456789)
0 commit comments