%r 是什么意思?
下面语句中的%r
是什么意思?
print '%r' % (1)
我想我听说过 %s
、%d
和 %f
但从未听说过这个。
What's the meaning of %r
in the following statement?
print '%r' % (1)
I think I've heard of %s
, %d
, and %f
but never heard of this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
背景:
在Python中,有两个内置函数用于将对象转换为字符串: < code>str 与
repr
。str
应该是一个友好的、人类可读的字符串。repr
应该包含有关对象内容的详细信息(有时,它们会返回相同的内容,例如整数)。按照惯例,如果有一个 Python 表达式将求值为另一个 == 对象,则repr
将返回这样的表达式,例如,如果返回表达式对于对象没有意义,则
repr< /code> 应该返回一个由 < 包围的字符串。和>符号例如
。要回答您原来的问题:
%s
<->str
%r
<->repr
另外:
您可以通过实现
来控制您自己的类的实例转换为字符串的方式__str__
和__repr__
方法。Background:
In Python, there are two builtin functions for turning an object into a string:
str
vs.repr
.str
is supposed to be a friendly, human readable string.repr
is supposed to include detailed information about an object's contents (sometimes, they'll return the same thing, such as for integers). By convention, if there's a Python expression that will eval to another object that's ==,repr
will return such an expression e.g.If returning an expression doesn't make sense for an object,
repr
should return a string that's surrounded by < and > symbols e.g.<blah>
.To answer your original question:
%s
<->str
%r
<->repr
In addition:
You can control the way an instance of your own classes convert to strings by implementing
__str__
and__repr__
methods.它在对象上调用
repr()
并插入结果字符串。It calls
repr()
on the object and inserts the resulting string.添加到上面给出的回复中,
'%r'
在您拥有具有异构数据类型的列表的情况下非常有用。假设我们有一个
list = [1, 'apple' , 2 , 'r','banana']
显然,在这种情况下使用
'%d'
或'%s'
会导致错误。相反,我们可以使用'%r'
来打印所有这些值。Adding to the replies given above,
'%r'
can be useful in a scenario where you have a list with heterogeneous data type.Let's say, we have a
list = [1, 'apple' , 2 , 'r','banana']
Obviously in this case using
'%d'
or'%s'
would cause an error. Instead, we can use'%r'
to print all these values.它使用
repr()
将替换内容打印为字符串。It prints the replacement as a string with
repr()
.%r
和%s
之间的区别在于,%r
调用的是repr()
方法,而%s
调用str()
方法。这两个都是 Python 内置函数。repr()
方法返回给定对象的可打印表示形式。str()
方法返回给定对象的“非正式”或可良好打印的表示形式。用简单的语言来说,
str()
方法的作用是以最终用户希望看到的方式打印结果:repr()
方法将打印或显示一个物体实际上是什么样子的:The difference between
%r
and%s
is,%r
calls therepr()
method and%s
calls thestr()
method. Both of these are built-in Python functions.The
repr()
method returns a printable representation of the given object.The
str()
method returns the "informal" or nicely printable representation of a given object.In simple language, what the
str()
method does is print the result in a way which the end user would like to see:The
repr()
method would print or show what an object actually looks like:%r
调用repr()
到对象上,并插入由
__repr__
返回的结果字符串。__repr__
返回的字符串应该是明确的,并且如果可能的话,与重新创建所表示的对象所需的源代码相匹配。一个简单的例子:
所以,
%r
callsrepr()
on the object, and inserts the resulting string returned by__repr__
.The string returned by
__repr__
should be unambiguous and, if possible, match the source code necessary to recreate the object being represented.A quick example:
So,
%s
调用所选对象的__str()__
方法并用返回值替换自身,%r
调用__repr所选对象的 ()__
方法并用返回值替换其自身。%s
calls the__str()__
method of the selected object and replaces itself with the return value,%r
calls the__repr()__
method of the selected object and replaces itself with the return value.请参阅文档中的字符串格式化操作。请注意,如果您习惯了它们在其他语言(例如 C)中的工作方式,%s 和 %d 等的工作方式可能与您期望的方式不同。
特别是,%s 也适用于整数和浮点数,除非您有特殊的格式要求其中 %d 或 %f 会给你更多的控制权。
See String Formatting Operations in the docs. Notice that %s and %d etc, might work differently to how you expect if you are used to the way they work in another language such as C.
In particular, %s also works well for ints and floats unless you have special formatting requirements where %d or %f will give you more control.
我读过《Learning Python the Hard Way》,作者说
I read in "Learning Python the Hard Way", the author said that