@@ -319,14 +319,16 @@ non-empty format specification typically modifies the result.
319319The general form of a *standard format specifier * is:
320320
321321.. productionlist :: format-spec
322- format_spec: [[`fill `]`align`][`sign `]["z"]["#"]["0"][`width `][`grouping_option `]["." `precision `][`type `]
322+ format_spec: [`options `][`width `][`grouping `]["." `precision `][`type `]
323+ options: [[`fill `]`align`][`sign `]["z"]["#"]["0"]
323324 fill: <any character>
324325 align: "<" | ">" | "=" | "^"
325326 sign: "+" | "-" | " "
326327 width: `~python-grammar:digit`+
327- grouping_option : "_ " | ", "
328+ grouping : ", " | "_ "
328329 precision: `~python-grammar:digit`+
329- type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
330+ type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g"
331+ : | "G" | "n" | "o" | "s" | "x" | "X" | "%"
330332
331333If a valid *align * value is specified, it can be preceded by a *fill *
332334character that can be any character and defaults to a space if omitted.
@@ -380,13 +382,13 @@ following:
380382+---------+----------------------------------------------------------+
381383| Option | Meaning |
382384+=========+==========================================================+
383- | ``'+' `` | indicates that a sign should be used for both |
385+ | ``'+' `` | Indicates that a sign should be used for both |
384386| | positive as well as negative numbers. |
385387+---------+----------------------------------------------------------+
386- | ``'-' `` | indicates that a sign should be used only for negative |
388+ | ``'-' `` | Indicates that a sign should be used only for negative |
387389| | numbers (this is the default behavior). |
388390+---------+----------------------------------------------------------+
389- | space | indicates that a leading space should be used on |
391+ | space | Indicates that a leading space should be used on |
390392| | positive numbers, and a minus sign on negative numbers. |
391393+---------+----------------------------------------------------------+
392394
@@ -414,30 +416,7 @@ decimal-point character appears in the result of these conversions
414416only if a digit follows it. In addition, for ``'g' `` and ``'G' ``
415417conversions, trailing zeros are not removed from the result.
416418
417- .. index :: single: , (comma); in string formatting
418-
419- The ``',' `` option signals the use of a comma for a thousands separator for
420- floating-point presentation types and for integer presentation type ``'d' ``.
421- For other presentation types, this option is an error.
422- For a locale aware separator, use the ``'n' `` integer presentation type
423- instead.
424-
425- .. versionchanged :: 3.1
426- Added the ``',' `` option (see also :pep: `378 `).
427-
428- .. index :: single: _ (underscore); in string formatting
429-
430- The ``'_' `` option signals the use of an underscore for a thousands
431- separator for floating-point presentation types and for integer
432- presentation type ``'d' ``. For integer presentation types ``'b' ``,
433- ``'o' ``, ``'x' ``, and ``'X' ``, underscores will be inserted every 4
434- digits. For other presentation types, specifying this option is an
435- error.
436-
437- .. versionchanged :: 3.6
438- Added the ``'_' `` option (see also :pep: `515 `).
439-
440- *width * is a decimal integer defining the minimum total field width,
419+ The *width * is a decimal integer defining the minimum total field width,
441420including any prefixes, separators, and other formatting characters.
442421If not specified, then the field width will be determined by the content.
443422
@@ -450,6 +429,42 @@ excluding :class:`complex`. This is equivalent to a *fill* character of
450429 Preceding the *width * field by ``'0' `` no longer affects the default
451430 alignment for strings.
452431
432+
433+ The *grouping * option after the *width * field specifies
434+ a digit group separator for the integral part of a number.
435+ It can be one of the following:
436+
437+ .. index ::
438+ single: , (comma); in string formatting
439+ single: _ (underscore); in string formatting
440+
441+ +---------+----------------------------------------------------------+
442+ | Option | Meaning |
443+ +=========+==========================================================+
444+ | ``',' `` | Inserts a comma every 3 digits for |
445+ | | integer presentation type ``'d' `` and |
446+ | | floating-point presentation types, excluding ``'n' ``. |
447+ | | For other presentation types, |
448+ | | this option is not supported. |
449+ +---------+----------------------------------------------------------+
450+ | ``'_' `` | Inserts an underscore every 3 digits for |
451+ | | integer presentation type ``'d' `` and |
452+ | | floating-point presentation types, excluding ``'n' ``. |
453+ | | For integer presentation types |
454+ | | ``'b' ``, ``'o' ``, ``'x' ``, and ``'X' ``, |
455+ | | underscores are inserted every 4 digits. |
456+ | | For other presentation types, |
457+ | | this option is not supported. |
458+ +---------+----------------------------------------------------------+
459+
460+ For a locale aware separator, use the ``'n' `` presentation type instead.
461+
462+ .. versionchanged :: 3.1
463+ Added the ``',' `` option (see also :pep: `378 `).
464+
465+ .. versionchanged :: 3.6
466+ Added the ``'_' `` option (see also :pep: `515 `).
467+
453468The *precision * is a decimal integer indicating how many digits should be
454469displayed after the decimal point for presentation types
455470``'f' `` and ``'F' ``, or before and after the decimal point for presentation
@@ -495,7 +510,7 @@ The available integer presentation types are:
495510 +---------+----------------------------------------------------------+
496511 | ``'n' `` | Number. This is the same as ``'d' ``, except that it uses |
497512 | | the current locale setting to insert the appropriate |
498- | | number separator characters. |
513+ | | digit group separators. |
499514 +---------+----------------------------------------------------------+
500515 | None | The same as ``'d' ``. |
501516 +---------+----------------------------------------------------------+
@@ -577,7 +592,8 @@ The available presentation types for :class:`float` and
577592 +---------+----------------------------------------------------------+
578593 | ``'n' `` | Number. This is the same as ``'g' ``, except that it uses |
579594 | | the current locale setting to insert the appropriate |
580- | | number separator characters. |
595+ | | digit group separators |
596+ | | for the integral part of a number. |
581597 +---------+----------------------------------------------------------+
582598 | ``'%' `` | Percentage. Multiplies the number by 100 and displays |
583599 | | in fixed (``'f' ``) format, followed by a percent sign. |
@@ -704,10 +720,16 @@ Replacing ``%x`` and ``%o`` and converting the value to different bases::
704720 >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
705721 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
706722
707- Using the comma as a thousands separator::
723+ Using the comma or the underscore as a digit group separator::
708724
709725 >>> '{:,}'.format(1234567890)
710726 '1,234,567,890'
727+ >>> '{:_}'.format(1234567890)
728+ '1_234_567_890'
729+ >>> '{:_b}'.format(1234567890)
730+ '100_1001_1001_0110_0000_0010_1101_0010'
731+ >>> '{:_x}'.format(1234567890)
732+ '4996_02d2'
711733
712734Expressing a percentage::
713735
0 commit comments