@@ -325,11 +325,11 @@ The general form of a *standard format specifier* is:
325
325
align: "<" | ">" | "=" | "^"
326
326
sign: "+" | "-" | " "
327
327
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 `]
330
330
width: `~python-grammar:digit`+
331
- grouping_option: "_" | ","
332
331
precision: `~python-grammar:digit`+
332
+ grouping: "," | "_"
333
333
type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g"
334
334
: | "G" | "n" | "o" | "s" | "x" | "X" | "%"
335
335
@@ -385,13 +385,13 @@ following:
385
385
+---------+----------------------------------------------------------+
386
386
| Option | Meaning |
387
387
+=========+==========================================================+
388
- | ``'+' `` | indicates that a sign should be used for both |
388
+ | ``'+' `` | Indicates that a sign should be used for both |
389
389
| | positive as well as negative numbers. |
390
390
+---------+----------------------------------------------------------+
391
- | ``'-' `` | indicates that a sign should be used only for negative |
391
+ | ``'-' `` | Indicates that a sign should be used only for negative |
392
392
| | numbers (this is the default behavior). |
393
393
+---------+----------------------------------------------------------+
394
- | space | indicates that a leading space should be used on |
394
+ | space | Indicates that a leading space should be used on |
395
395
| | positive numbers, and a minus sign on negative numbers. |
396
396
+---------+----------------------------------------------------------+
397
397
@@ -419,30 +419,7 @@ decimal-point character appears in the result of these conversions
419
419
only if a digit follows it. In addition, for ``'g' `` and ``'G' ``
420
420
conversions, trailing zeros are not removed from the result.
421
421
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,
446
423
including any prefixes, separators, and other formatting characters.
447
424
If not specified, then the field width will be determined by the content.
448
425
@@ -463,12 +440,43 @@ indicates the maximum field size - in other words, how many characters will be
463
440
used from the field content. The *precision * is not allowed for integer
464
441
presentation types.
465
442
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 `).
469
477
470
478
.. versionchanged :: 3.14
471
- Support thousands separators for the fractional part.
479
+ Support the * grouping * option for the fractional part.
472
480
473
481
Finally, the *type * determines how the data should be presented.
474
482
@@ -507,7 +515,7 @@ The available integer presentation types are:
507
515
+---------+----------------------------------------------------------+
508
516
| ``'n' `` | Number. This is the same as ``'d' ``, except that it uses |
509
517
| | the current locale setting to insert the appropriate |
510
- | | number separator characters. |
518
+ | | digit group separators. |
511
519
+---------+----------------------------------------------------------+
512
520
| None | The same as ``'d' ``. |
513
521
+---------+----------------------------------------------------------+
@@ -589,7 +597,8 @@ The available presentation types for :class:`float` and
589
597
+---------+----------------------------------------------------------+
590
598
| ``'n' `` | Number. This is the same as ``'g' ``, except that it uses |
591
599
| | the current locale setting to insert the appropriate |
592
- | | number separator characters. |
600
+ | | digit group separators |
601
+ | | for the integral part of a number. |
593
602
+---------+----------------------------------------------------------+
594
603
| ``'%' `` | Percentage. Multiplies the number by 100 and displays |
595
604
| | in fixed (``'f' ``) format, followed by a percent sign. |
@@ -716,12 +725,16 @@ Replacing ``%x`` and ``%o`` and converting the value to different bases::
716
725
>>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
717
726
'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
718
727
719
- Using the comma or the underscore as a thousands separator::
728
+ Using the comma or the underscore as a digit group separator::
720
729
721
730
>>> '{:,}'.format(1234567890)
722
731
'1,234,567,890'
723
732
>>> '{:_}'.format(1234567890)
724
733
'1_234_567_890'
734
+ >>> '{:_b}'.format(1234567890)
735
+ '100_1001_1001_0110_0000_0010_1101_0010'
736
+ >>> '{:_x}'.format(1234567890)
737
+ '4996_02d2'
725
738
>>> '{:_}'.format(123456789.123456789)
726
739
'123_456_789.12345679'
727
740
>>> '{:._}'.format(123456789.123456789)
0 commit comments