@@ -319,14 +319,16 @@ non-empty format specification typically modifies the result.
319
319
The general form of a *standard format specifier * is:
320
320
321
321
.. 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"]
323
324
fill: <any character>
324
325
align: "<" | ">" | "=" | "^"
325
326
sign: "+" | "-" | " "
326
327
width: `~python-grammar:digit`+
327
- grouping_option : "_ " | ", "
328
+ grouping : ", " | "_ "
328
329
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" | "%"
330
332
331
333
If a valid *align * value is specified, it can be preceded by a *fill *
332
334
character that can be any character and defaults to a space if omitted.
@@ -380,13 +382,13 @@ following:
380
382
+---------+----------------------------------------------------------+
381
383
| Option | Meaning |
382
384
+=========+==========================================================+
383
- | ``'+' `` | indicates that a sign should be used for both |
385
+ | ``'+' `` | Indicates that a sign should be used for both |
384
386
| | positive as well as negative numbers. |
385
387
+---------+----------------------------------------------------------+
386
- | ``'-' `` | indicates that a sign should be used only for negative |
388
+ | ``'-' `` | Indicates that a sign should be used only for negative |
387
389
| | numbers (this is the default behavior). |
388
390
+---------+----------------------------------------------------------+
389
- | space | indicates that a leading space should be used on |
391
+ | space | Indicates that a leading space should be used on |
390
392
| | positive numbers, and a minus sign on negative numbers. |
391
393
+---------+----------------------------------------------------------+
392
394
@@ -414,30 +416,7 @@ decimal-point character appears in the result of these conversions
414
416
only if a digit follows it. In addition, for ``'g' `` and ``'G' ``
415
417
conversions, trailing zeros are not removed from the result.
416
418
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,
441
420
including any prefixes, separators, and other formatting characters.
442
421
If not specified, then the field width will be determined by the content.
443
422
@@ -450,6 +429,42 @@ excluding :class:`complex`. This is equivalent to a *fill* character of
450
429
Preceding the *width * field by ``'0' `` no longer affects the default
451
430
alignment for strings.
452
431
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
+
453
468
The *precision * is a decimal integer indicating how many digits should be
454
469
displayed after the decimal point for presentation types
455
470
``'f' `` and ``'F' ``, or before and after the decimal point for presentation
@@ -495,7 +510,7 @@ The available integer presentation types are:
495
510
+---------+----------------------------------------------------------+
496
511
| ``'n' `` | Number. This is the same as ``'d' ``, except that it uses |
497
512
| | the current locale setting to insert the appropriate |
498
- | | number separator characters. |
513
+ | | digit group separators. |
499
514
+---------+----------------------------------------------------------+
500
515
| None | The same as ``'d' ``. |
501
516
+---------+----------------------------------------------------------+
@@ -577,7 +592,8 @@ The available presentation types for :class:`float` and
577
592
+---------+----------------------------------------------------------+
578
593
| ``'n' `` | Number. This is the same as ``'g' ``, except that it uses |
579
594
| | the current locale setting to insert the appropriate |
580
- | | number separator characters. |
595
+ | | digit group separators |
596
+ | | for the integral part of a number. |
581
597
+---------+----------------------------------------------------------+
582
598
| ``'%' `` | Percentage. Multiplies the number by 100 and displays |
583
599
| | in fixed (``'f' ``) format, followed by a percent sign. |
@@ -704,10 +720,16 @@ Replacing ``%x`` and ``%o`` and converting the value to different bases::
704
720
>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
705
721
'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
706
722
707
- Using the comma as a thousands separator::
723
+ Using the comma or the underscore as a digit group separator::
708
724
709
725
>>> '{:,}'.format(1234567890)
710
726
'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'
711
733
712
734
Expressing a percentage::
713
735
0 commit comments