>>>print('You say:{0:r}'.format("i love you"))
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print('You say:{0:r}'.format("i love you"))
ValueError: Unknown format code 'r' for object of type 'str'
我只是在python2中使用%r(repr())
,它应该在python3.5中起作用。为什么?
另外,我应该使用什么格式?
你要找的是转换标志。应该这样说明
引用Python 3的official documentation
请注意,Python 2只支持
!s
和!r
。根据Python 2的official documentation在Python 2中,您可能做了如下事情
但即使在Python 2中(也在Python 3中),也可以用
!r
和format
编写相同的代码,如下所示引用official documentation的例子
相关问题 更多 >
编程相关推荐