我的印象是,在Python中,用r'this is a raw string'编写的原始字符串将省略任何转义字符,并准确地在引号之间打印。我的问题是,当我尝试print r'\'时,我得到了SyntaxError: EOL while scanning string literal。不过,print r'\n'正确地打印了\n。
我阅读了一些关于python错误的信息,并找到了。作者给出了一个正确的实例,但是我不知道字符串"""\\"Axis of Awesome\\\""""是如何工作的。有人能帮我解释一下字符串interrupt.Thanks是怎样的吗?
==================================================
答案是:我想了很多,终于想明白了。__repr__对字符串执行__str__函数并输出\\"Axis of Awesome\\",,但是在超级无聊的解释中,字符串上的__str__函数
请告诉我为什么我的代码不能像我想的那样在python3.4中工作:
r = input("file address")
file = open(r)
for line in file:
if line.endswith("d") is True:
print(line)
我的文件是一个*.txt,包含以下几行:
This line ends with a
this line ends with b
this line ends with c
this line ends with d
我希望python打印出最后一个,因为它的末尾有
预期的工作如下:
>>> print re.sub('(\w)"(\W)', r"\1''\2", 'The "raw string literal" is a special case of a "string literal".')
The "raw string literal'' is a special case of a "string literal''.
因为我想在替换表达式中使用单引号(这是正确的术语吗?),
我注意到,如果我将“崇高文本3”中的一长行Python与注释分开,那么尽管脚本运行良好,它还是将前两行标记为红色(因此是错误的)。
# This is a long line broken up with comments
"So I'm ready to attack, gonna lead the pack," + \ # red comment here
"Gonna get a touchdown, gonna take you out," + \ # and here
"That's right, put your pom-p
我知道这是一个菜鸟问题,但对于Python来说是个新问题,并试图理解以下内容:
为什么反斜杠转义在这里工作时没有错误:
>>> print "this is \\\ a string"
this is \\ a string
但当我尝试:
>>> print "\\\"
我得到:
SyntaxError: EOL while scanning string literal