Python 初学者问题 RE:字符串格式化类型和 repr() 函数
所以我有这样的代码:
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
我得到这个输出:
1 2 3 4
'一' '二' '三' '四'
我的问题是:
为什么输出的第二行有单引号?我不太确定 %r 转换类型到底是如何工作的。
当我将代码更改为:
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print "%s %s %s %s" % ("one", "two", "three", "four")
我得到这个结果:
1 2 3 4
一二三四
我只是不明白为什么他们工作方式不同。有人可以帮我分解它吗?
我读过:
So I have this code:
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
And I get this output:
1 2 3 4
'one' 'two' 'three' 'four'
My question is:
Why does the second line of output have single quotes around it? I'm not quite sure how the %r conversion type really works.
When I change the code to:
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print "%s %s %s %s" % ("one", "two", "three", "four")
I get this result:
1 2 3 4
one two three four
I just don't understand why they work differently. Can someone break it down for me?
I've read:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于表达式
'abc%rdef' % obj
,部分'%r'
替换为repr(obj)
对于表达式
'ABC%sDEF' % obj
,部分'%s'
替换为str(obj)
。
repr() 是一个函数,对于常见对象,它返回一个字符串,该字符串与您在脚本中编写的字符串相同,用于定义作为参数传递给 repr() 的对象功能:
。
如果考虑
li = [12,45,'haze']
定义的列表print li
将打印 [12,45,'haze']print repr(li)
也会打印 [12,45,'haze'] ,因为[12,45,'haze']< /code> 是序列在脚本中编写的字符,用于定义具有此值的列表 li
如果您考虑由
ss = 'oregon'
定义的字符串:周围没有任何引号
print ss
将打印 oregon ,并且print repr(ss)
将打印 'oregon' ,因为'oregon'
是如果您想定义字符串ss程序中的值为 oregon。
因此,这意味着,事实上,对于常见对象,repr() 和 str() 返回的字符串通常是相等的,但字符串对象除外。这使得 repr() 对于字符串对象特别有趣。例如,分析 HTML 代码的内容非常有用。
With the expression
'abc%rdef' % obj
, the part'%r'
is replaced withrepr(obj)
With the expression
'ABC%sDEF' % obj
, the part'%s'
is replaced withstr(obj)
.
repr() is a function that , for common objects, returns a string that is the same as the one you would write in a script to define the object passed as argument to the repr() function:
.
if you consider the list defined by
li = [12,45,'haze']
print li
will print [12,45,'haze']print repr(li)
will also print [12,45,'haze'] , because[12,45,'haze']
is the sequence of characters that are written in a script to define the list li with this valueif you consider the string defined by
ss = 'oregon'
:print ss
will print oregon , without any quote aroundprint repr(ss)
will print 'oregon' , since'oregon'
is the sequence of characters that you must write in a script if you want to define the string ss with the value oregon in a program.
So, this means that , in fact, for common objects, repr() and str() return strings that are in general equal, except for a string object. That makes repr() particularly interesting for string objects. It is very useful to analyse the contents of HTML codes, for exemple.
%s
告诉 python 对元组的每个元素调用str()
函数。%r
告诉 python 对元组的每个元素调用repr()
函数。通过文档:
str():
repr():
这意味着,如果您调用 repr() 的对象是一个字符串对象(在 python 中一切都是对象),它会向您显示不同字符的表示方式。
@你的具体问题:我假设“...”表明它是一个字符串。如果您对 int 调用 repr() ,则不会有“...”。
%s
tell's python to call thestr()
function on each element of your tuple.%r
tell's python to call therepr()
function on each element of your tuple.By the docs:
str():
repr():
This means, if the object you call
repr()
on is a string object (in python everything is an object) it shows you how the different characters are represented.@your specific question: I assume that the "..." indicate, that it is a string. If you call repr() on an int there are no "...".