What Is The Meaning Of Colon In Python's String Format?
In the reading of Python's Format Specification Mini-Language, format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] fill ::= al
Solution 1:
You are only looking at the grammar for the format_spec, the full grammar is specified higher up on the same page:
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
arg_name ::= [identifier | integer]
attribute_name ::= identifier
element_index ::= integer | index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s"
format_spec ::= <described in the next section>
In the replacement_field syntax notice the : preceding the format_spec.
The
field_nameis optionally followed by a conversion field, which is preceded by an exclamation point'!', and aformat_spec, which is preceded by a colon':'
When the field_name and/or conversion are specified, : marks the end of former and the start of the format_spec.
In your example,
>>> "{0:b}".format(100)
'1100100'zero specifies the optional field_name which in this case corresponds to the index of the item to be formatted in the passed parameter tuple; it is optional so it can be dropped.
Post a Comment for "What Is The Meaning Of Colon In Python's String Format?"