艰难地学习 Python,练习 10.4

发布于 2024-12-10 10:44:44 字数 678 浏览 0 评论 0原文

#Combine %r with double-quote and single-quote escapes and print them out. 
#Compare %r with %s. Notice how %r prints it the way you'd write it in your file, 
#but %s  prints it the way you'd like to see it?

这是我为练习编写的内容:

1) mybugs1 = "Guido said: %r and moved on." %'I \'love\' \"my\" bugs'

2) mybugs2 = "Stallman said: %s and moved on." % 'I \'love\' \"my\" bugs'

3) print mybugs1

4) print mybugs2 

输出

Guido said: 'I \'love\' "my" bugs' and moved on.

Stallman said: I 'love' "my" bugs and moved on.

问题

%r 不会按照我在 .py 文件中编写的方式打印所有内容(例如,它在第 1 行打印“my”而不是“my”)。为什么?

#Combine %r with double-quote and single-quote escapes and print them out. 
#Compare %r with %s. Notice how %r prints it the way you'd write it in your file, 
#but %s  prints it the way you'd like to see it?

This is what I wrote for the exercise:

1) mybugs1 = "Guido said: %r and moved on." %'I \'love\' \"my\" bugs'

2) mybugs2 = "Stallman said: %s and moved on." % 'I \'love\' \"my\" bugs'

3) print mybugs1

4) print mybugs2 

Output

Guido said: 'I \'love\' "my" bugs' and moved on.

Stallman said: I 'love' "my" bugs and moved on.

Question

%r doesn't print everything the way I'd write it in my .py file (for example it prints "my" instead of \"my\" at line 1). Why?

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

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

发布评论

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

评论(2

も星光 2024-12-17 10:44:44

在 python 中编写字符串文字的方法有多种,包括单引号或双引号、单行或多行、原始或普通。不过,这些都没有保留在字符串本身中;一旦解析器解析它,就不会保留有关它如何在源代码中呈现的信息;它甚至不需要在源代码中,您可以动态生成字符串,例如通过从文件中读取字符串、询问用户或将数字转换为字符串。

因此,当您repr()一个字符串时,Python猜测采用哪种方式将其格式化为看起来像文字。它使用的规则很简单,如果字符串包含单引号但不包含双引号,则使用单行、双引号、非原始文字;在所有其他情况下,它使用单行、单引号、非原始文字;换句话说,Python 更喜欢单引号,但如果它格式化的字符串包含单引号,但没有双引号,它可以通过使用双引号字符串来 repr() 该字符串,而无需反斜杠转义引号。

请记住,repr() 不会返回您输入的内容,因为它不知道您输入的内容;您可能永远不会全部输入。它返回一些可以解析回相同值的东西。身份*是:

x == eval(repr(x))

不是

x == repr(eval(x))

* repr()对此也不神奇,并非所有对象都以这样的方式实现__repr__保留了这个约束。 repr 主要用于提供有用的调试信息,而不是用于生成 python 代码

there are several ways to write string literals in python, with single or double quotes, single-line or multiline, raw or normal. None of that is preserved in the string itself, though; once the parser parses it, no information remains about how it was presented in the source; it doesn't even need to be in the source, you could generate the string dynamically, say, by reading it from a file, asking the user, or turning a number into a string.

So when you repr() a string, python guesses which way to format it to look like a literal. The rules it uses are simple, if the string contains single quotes but no double quotes, it uses a single-line, double-quote, non-raw literal; In all other cases, it uses a single-line, single-quote, non-raw literal; Stated another way, python prefers single quotes, but if it's formatting a string that has single quotes, but not double quotes, it can repr() that string without backslash escaping the quotes by using a double quoted string.

remember, repr() doesn't return what you typed, because it doesn't know what you typed; you might never have typed it all. It returns something that can be parsed back into the same value. The identity* is:

x == eval(repr(x))

not

x == repr(eval(x))

* repr() is not magical for this either, not all objects implement __repr__ in a way that preserves this constraint. repr is mostly used for providing useful debugging information, not for generating python code

千纸鹤 2024-12-17 10:44:44

单引号内的双引号不必转义,反之亦然。

解析器删除反斜杠。

例子

>>> s = 'I \'love\' \"my\" bugs'
>>> s
'I \'love\' "my" bugs'
>>> 

Double-quotes inside single-quotes don't have to be escaped, and vice versa.

The parser removes the backslash.

Example

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