Python 初学者问题 RE:字符串格式化类型和 repr() 函数

发布于 2024-12-01 13:25:18 字数 850 浏览 2 评论 0原文

所以我有这样的代码:

    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

一二三四

我只是不明白为什么他们工作方式不同。有人可以帮我分解它吗?


我读过:

http://docs. python.org/library/stdtypes.html &

http://docs.python.org/library/functions.html#repr

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:

http://docs.python.org/library/stdtypes.html &

http://docs.python.org/library/functions.html#repr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2024-12-08 13:25:18

对于表达式 'abc%rdef' % obj ,部分 '%r' 替换为 repr(obj)

对于表达式 'ABC%sDEF' % obj ,部分 '%s' 替换为 str(obj)

repr() 是一个函数,对于常见对象,它返回一个字符串,该字符串与您在脚本中编写的字符串相同,用于定义作为参数传递给 repr() 的对象功能:

对于许多类型,此函数会尝试返回一个字符串
当传递给 eval() 时会产生一个具有相同值的对象
http://docs.python.org/library/functions.html#repr

示例1

如果考虑 li = [12,45,'haze'] 定义的列表

print li 将打印 [12,45,'haze']

print repr(li) 也会打印 [12,45,'haze'] ,因为 [12,45,'haze']< /code> 是序列在脚本中编写的字符,用于定义具有此值的列表 li

示例2

如果您考虑由 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 with repr(obj)

With the expression 'ABC%sDEF' % obj , the part '%s' is replaced with str(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:

For many types, this function makes an attempt to return a string that
would yield an object with the same value when passed to eval()
http://docs.python.org/library/functions.html#repr

.

Example 1

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 value

Example 2

if you consider the string defined by ss = 'oregon' :

print ss will print oregon , without any quote around

print 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.

jJeQQOZ5 2024-12-08 13:25:18

%s 告诉 python 对元组的每个元素调用 str() 函数。 %r 告诉 python 对元组的每个元素调用 repr() 函数。

通过文档:

str()

返回包含nicely可打印的字符串
对象的表示。对于字符串,这将返回字符串
本身。

repr()

返回一个包含对象的可打印表示形式的字符串。

这意味着,如果您调用 repr() 的对象是一个字符串对象(在 python 中一切都是对象),它会向您显示不同字符的表示方式。

@你的具体问题:我假设“...”表明它是一个字符串。如果您对 int 调用 repr() ,则不会有“...”。

%s tell's python to call the str() function on each element of your tuple. %r tell's python to call the repr() function on each element of your tuple.

By the docs:

str():

Return a string containing a nicely printable
representation of an object. For strings, this returns the string
itself.

repr():

Return a string containing a printable representation of an object.

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 "...".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文