When {? Does S} make sense in format ()?

>>> import math
>>> print "{} {!s} {!r}".format(math.pi, math.pi, math.pi)
3.14159265359 3.14159265359 3.141592653589793

When is !sinside {}significant?

Or, in other words, when do you call str()in an argument format()meaningful?

+3
source share
1 answer

Whenever a printable type implements __format__inappropriately for your purpose. In this case, you can use !sor !r(and I think there is even !a) to indicate an alternative formatter.

Of course, you won’t need to print floating point numbers, but probably for custom types.

+2
source

All Articles