Skip to content

Commit 06db02f

Browse files
[3.13] gh-106318: Add example for str.format() (GH-137018) (#141903)
Co-authored-by: Adorilson Bezerra <adorilson@gmail.com>
1 parent a676b2e commit 06db02f

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Doc/library/stdtypes.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,10 +1858,16 @@ expression support in the :mod:`re` module).
18581858
``{}``. Each replacement field contains either the numeric index of a
18591859
positional argument, or the name of a keyword argument. Returns a copy of
18601860
the string where each replacement field is replaced with the string value of
1861-
the corresponding argument.
1861+
the corresponding argument. For example:
1862+
1863+
.. doctest::
18621864

18631865
>>> "The sum of 1 + 2 is {0}".format(1+2)
18641866
'The sum of 1 + 2 is 3'
1867+
>>> "The sum of {a} + {b} is {answer}".format(answer=1+2, a=1, b=2)
1868+
'The sum of 1 + 2 is 3'
1869+
>>> "{1} expects the {0} Inquisition!".format("Spanish", "Nobody")
1870+
'Nobody expects the Spanish Inquisition!'
18651871

18661872
See :ref:`formatstrings` for a description of the various formatting options
18671873
that can be specified in format strings.

0 commit comments

Comments
 (0)