请问这个语句是输出什么? print('{!r}'.format(x))

2017-11-13 22:16:45 +08:00
 saximi
{!r}是指以什么样的格式输出呢?
谢谢了!
3937 次点击
所在节点    Python
5 条回复
ipwx
2017-11-13 23:30:35 +08:00
repr(x)
enrolls
2017-11-14 02:43:14 +08:00
善用谷歌啊。https://pyformat.info/
zhusimaji
2017-11-14 08:40:39 +08:00
是时候了解一下 repr 与 str 区别了
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.
saximi
2017-11-15 20:50:40 +08:00
@ipwx
@enrolls
@zhusimaji
@bxtx999 感谢大家指点!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/406110

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX