艰难地学习 Python,练习 10.4
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 python 中编写字符串文字的方法有多种,包括单引号或双引号、单行或多行、原始或普通。不过,这些都没有保留在字符串本身中;一旦解析器解析它,就不会保留有关它如何在源代码中呈现的信息;它甚至不需要在源代码中,您可以动态生成字符串,例如通过从文件中读取字符串、询问用户或将数字转换为字符串。
因此,当您
repr()
一个字符串时,Python猜测采用哪种方式将其格式化为看起来像文字。它使用的规则很简单,如果字符串包含单引号但不包含双引号,则使用单行、双引号、非原始文字;在所有其他情况下,它使用单行、单引号、非原始文字;换句话说,Python 更喜欢单引号,但如果它格式化的字符串包含单引号,但没有双引号,它可以通过使用双引号字符串来repr()
该字符串,而无需反斜杠转义引号。请记住,
repr()
不会返回您输入的内容,因为它不知道您输入的内容;您可能永远不会全部输入。它返回一些可以解析回相同值的东西。身份*是:不是
*
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 canrepr()
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:not
*
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单引号内的双引号不必转义,反之亦然。
解析器删除反斜杠。
例子
Double-quotes inside single-quotes don't have to be escaped, and vice versa.
The parser removes the backslash.
Example