1
ipwx 2017-11-13 23:30:35 +08:00
repr(x)
|
2
enrolls 2017-11-14 02:43:14 +08:00 1
善用谷歌啊。https://pyformat.info/
|
3
zhusimaji 2017-11-14 08:40:39 +08:00 via iPhone
是时候了解一下 repr 与 str 区别了
|
4
bxtx999 2017-11-14 09:07:29 +08:00
The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is not equivalent syntax). For objects which don ’ t have a particular representation for human consumption, str() will return the same value as repr().
'!a' (apply ascii()), '!s' (apply str()) and '!r' (apply repr()) can be used to convert the value before it is formatted: >>> import math >>> print('The value of PI is approximately {}.'.format(math.pi)) The value of PI is approximately 3.14159265359. >>> print('The value of PI is approximately {!r}.'.format(math.pi)) The value of PI is approximately 3.141592653589793. |