The grammar for a replacement field is as follows:
replacement_field ::= "{" , field_name [ ":" , format_spec ] "}"
field_name ::= arg_name { arg_dimension }
arg_dimension ::= "." , attribute_name | "[" , element_index , "]"
arg_name ::= identifier | integer
attribute_name ::= identifier
element_index ::= integer | index_string
index_string ::= #'[A-Za-z][A-Za-z0-9_+]*'
format_spec ::= #'[A-Za-z][A-Za-z0-9_+]*'
identifier ::= #'[A-Za-z][A-Za-z0-9_+]*'
integer ::= #'[0-9]+'
In less formal terms, the replacement field can start with a field_name
that specifies the object whose value is to be formatted and inserted into
the output instead of the replacement field.
The field_name is optionally followed by a conversion field, which is
preceded by an exclamation point '!', and a format_spec, which is
preceded by a colon ':'.
These specify a non-default format for the replacement value.
The field_name itself begins with an arg_name that is either a number or a keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword, it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, … in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, … will be automatically inserted in that order. Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings '10' or ':-]') within a format string. The arg_name can be followed by any number of index or attribute expressions. An expression of the form '.name' selects the named attribute using getattr(), while an expression of the form '[index]' does an index lookup using getitem().
The positional argument specifiers can be omitted, so {} {}
is equivalent
to {0} {1}
.