Python string format: When to use !s conversion flag

后端 未结 3 943
感动是毒
感动是毒 2020-12-17 09:28

What\'s the difference between these 2 string format statements in Python:

\'{0}\'.format(a)
\'{0!s}\'.format(a)

Both have the same output

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 10:05

    It is mentioned in the documentation:

    The conversion field causes a type coercion before formatting. Normally, the job of formatting a value is done by the __format__() method of the value itself. However, in some cases it is desirable to force a type to be formatted as a string, overriding its own definition of formatting. By converting the value to a string before calling __format__(), the normal formatting logic is bypassed.

    Two conversion flags are currently supported: '!s' which calls str() on the value, and '!r' which calls repr().

    An example can be taken (again from the documentation) to show the difference:

    >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
    "repr() shows quotes: 'test1'; str() doesn't: test2"
    

提交回复
热议问题