GeezerVison is showing github.com — gh-106318: Add example for str.format() (#137018) · StanFromIreland/cpython@6a5f5fd
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Doc/library/stdtypes.rst +7-1Lines changed: 7 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1994,10 +1994,16 @@ expression support in the :mod:`re` module). |
1994 | 1994 | ``{}``. Each replacement field contains either the numeric index of a |
1995 | 1995 | positional argument, or the name of a keyword argument. Returns a copy of |
1996 | 1996 | the string where each replacement field is replaced with the string value of |
1997 | | - the corresponding argument. |
| 1997 | + the corresponding argument. For example: |
| 1998 | + |
| 1999 | + .. doctest:: |
1998 | 2000 |
|
1999 | 2001 | >>> "The sum of 1 + 2 is {0}".format(1+2) |
2000 | 2002 | 'The sum of 1 + 2 is 3' |
| 2003 | + >>> "The sum of {a} + {b} is {answer}".format(answer=1+2, a=1, b=2) |
| 2004 | + 'The sum of 1 + 2 is 3' |
| 2005 | + >>> "{1} expects the {0} Inquisition!".format("Spanish", "Nobody") |
| 2006 | + 'Nobody expects the Spanish Inquisition!' |
2001 | 2007 |
|
2002 | 2008 | See :ref:`formatstrings` for a description of the various formatting options |
2003 | 2009 | that can be specified in format strings. |
|