Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4cae32

Browse files
committed
Merged revisions 71788 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r71788 | eric.smith | 2009-04-21 20:47:00 -0400 (Tue, 21 Apr 2009) | 1 line Documentation for issue 5237, auto-numbered format fields. Contributed by Terry J. Reedy. ........
1 parent b1a03cf commit c4cae32

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

Doc/ACKS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ [email protected]), and we'll be glad to correct the problem.
157157
* Paul Prescod
158158
* Eric S. Raymond
159159
* Edward K. Ream
160+
* Terry J. Reedy
160161
* Sean Reifschneider
161162
* Bernhard Reiter
162163
* Armin Rigo

Doc/library/string.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,26 @@ The grammar for a replacement field is as follows:
196196

197197
.. productionlist:: sf
198198
replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}"
199-
field_name: (`identifier` | `integer`) ("." `attribute_name` | "[" `element_index` "]")*
199+
field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")*
200+
arg_name: (`identifier` | `integer`)?
200201
attribute_name: `identifier`
201202
element_index: `integer`
202203
conversion: "r" | "s" | "a"
203204
format_spec: <described in the next section>
204205

205-
In less formal terms, the replacement field starts with a *field_name*, which
206-
can either be a number (for a positional argument), or an identifier (for
207-
keyword arguments). Following this is an optional *conversion* field, which is
206+
In less formal terms, the replacement field starts with a *field_name* that specifies
207+
the object whose value is to be formatted and inserted
208+
into the output instead of the replacement field.
209+
The *field_name* is optionally followed by a *conversion* field, which is
208210
preceded by an exclamation point ``'!'``, and a *format_spec*, which is preceded
209-
by a colon ``':'``.
210-
211-
The *field_name* itself begins with either a number or a keyword. If it's a
212-
number, it refers to a positional argument, and if it's a keyword it refers to a
213-
named keyword argument. This can be followed by any number of index or
211+
by a colon ``':'``. These specify a non-default format for the replacement value.
212+
213+
The *field_name* itself begins with an *arg_name* that is either either a number or a
214+
keyword. If it's a number, it refers to a positional argument, and if it's a keyword,
215+
it refers to a named keyword argument. If the numerical arg_names in a format string
216+
are 0, 1, 2, ... in sequence, they can all be omitted (not just some)
217+
and the numbers 0, 1, 2, ... will be automatically inserted in that order.
218+
The *arg_name* can be followed by any number of index or
214219
attribute expressions. An expression of the form ``'.name'`` selects the named
215220
attribute using :func:`getattr`, while an expression of the form ``'[index]'``
216221
does an index lookup using :func:`__getitem__`.
@@ -219,6 +224,7 @@ Some simple format string examples::
219224

220225
"First, thou shalt count to {0}" # References first positional argument
221226
"Bring me a {}" # Implicitly references the first positional argument
227+
"From {} to {}" # Same as "From {0] to {1}"
222228
"My quest is {name}" # References keyword argument 'name'
223229
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
224230
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.

0 commit comments

Comments
 (0)