What\'s the difference between these 2 string format statements in Python:
\'{0}\'.format(a)
\'{0!s}\'.format(a)
Both have the same output
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 callsstr()
on the value, and '!r
' which callsrepr()
.
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"