I am confused by the following behavior of Python 2.7 and Python 3.3 in string formatting. This is a question related to the detailed question of how the comma operator interacts with string types.
>>> format(10000, ",d")
'10,000'
>>> format(10000, ",")
'10,000'
>>> format(10000, ",s")
ValueError: Cannot specify ',' with 's'.
>>> "{:,}".format(10000)
'10,000'
>>> "{:,s}".format(10000)
ValueError: Cannot specify ',' with 's'.
What bothers me, why does the option work ,that does not have an explicit type of string representation. docs say that if you omit the type, it is "Same as s". And yet here he acts differently than s.
I would throw it as an argument against wrinkles / angles, but this syntax is used as an example in the documents '{:,}'.format(1234567890). Are there other "special" behaviors hidden in Python when the string representation type is omitted? Maybe instead of “the same as s”, what the code really does is to check the type of the formatted thing?
source
share