0

A word in the python documentation Format Specification Mini-Language:

A general convention is that an empty format string ("") produces the same result as if you had called str() on the value.

But it doesn't match the actual result from both python2 and python3:

In [1]: "".format(100)
Out[1]: ''

In [2]: str(100)
Out[2]: '100'
1
  • 1
    format(100, '') == '100', that's what it means. The way you're doing it you need '{}'.format(100), which has a placeholder with an empty format string. Commented Jan 10, 2017 at 11:08

1 Answer 1

4

You have an empty template, not an empty format string. The format string is the part after the optional : in a {..} placeholder. By completely omitting the placeholder, there is nowhere for the value to placed into.

So the following produces the same as str() on the value:

>>> '{:}'.format(100)
'100'
>>> '{}'.format(100)
'100'

as does the empty string as a second argument to the format() function:

>>> format(100, '')
'100'

In all cases the format string is empty.

You may have missed that the whole Format Specification Mini-language only documents what formatting operations you can use in the {:...} part of a placeholder, or as the second argument for format(). For template strings (the part you apply the str.format() method to), you need to read section above that, the Format String Syntax section.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.