@@ -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
208210preceded 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
214219attribute expressions. An expression of the form ``'.name' `` selects the named
215220attribute using :func: `getattr `, while an expression of the form ``'[index]' ``
216221does 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