Skip to content

Commit d268bd0

Browse files
miss-islingtonskirpichevpicnixz
authored
[3.12] gh-122767: document "new style" formatting for complexes (GH-122848) (#126129)
gh-122767: document "new style" formatting for complexes (GH-122848) (cherry picked from commit 0bbbe15) Co-authored-by: Sergey B Kirpichev <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent e8ce872 commit d268bd0

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Doc/library/string.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,9 @@ The meaning of the various alignment options is as follows:
350350
| ``'='`` | Forces the padding to be placed after the sign (if any) |
351351
| | but before the digits. This is used for printing fields |
352352
| | in the form '+000000120'. This alignment option is only |
353-
| | valid for numeric types. It becomes the default for |
354-
| | numbers when '0' immediately precedes the field width. |
353+
| | valid for numeric types, excluding :class:`complex`. |
354+
| | It becomes the default for numbers when '0' immediately |
355+
| | precedes the field width. |
355356
+---------+----------------------------------------------------------+
356357
| ``'^'`` | Forces the field to be centered within the available |
357358
| | space. |
@@ -432,9 +433,9 @@ including any prefixes, separators, and other formatting characters.
432433
If not specified, then the field width will be determined by the content.
433434

434435
When no explicit alignment is given, preceding the *width* field by a zero
435-
(``'0'``) character enables
436-
sign-aware zero-padding for numeric types. This is equivalent to a *fill*
437-
character of ``'0'`` with an *alignment* type of ``'='``.
436+
(``'0'``) character enables sign-aware zero-padding for numeric types,
437+
excluding :class:`complex`. This is equivalent to a *fill* character of
438+
``'0'`` with an *alignment* type of ``'='``.
438439

439440
.. versionchanged:: 3.10
440441
Preceding the *width* field by ``'0'`` no longer affects the default
@@ -588,6 +589,15 @@ The available presentation types for :class:`float` and
588589
| | as altered by the other format modifiers. |
589590
+---------+----------------------------------------------------------+
590591

592+
The available presentation types for :class:`complex` are the same as those for
593+
:class:`float` (``'%'`` is not allowed). Both the real and imaginary components
594+
of a complex number are formatted as floating-point numbers, according to the
595+
specified presentation type. They are separated by the mandatory sign of the
596+
imaginary part, the latter being terminated by a ``j`` suffix. If the presentation
597+
type is missing, the result will match the output of :func:`str` (complex numbers with
598+
a non-zero real part are also surrounded by parentheses), possibly altered by
599+
other format modifiers.
600+
591601

592602
.. _formatexamples:
593603

0 commit comments

Comments
 (0)