从技术上讲,任何奇数个反斜杠,如文档中所述。
>>> r'\'
File "<stdin>", line 1
r'\'
^
SyntaxError: EOL while scanning string literal
>>> r'\\'
'\\\\'
>>> r'\\\'
File "<stdin>", line 1
r'\\\'
^
SyntaxError: EOL while scanning string literal
解析器似乎只能将原始字符串中的反斜杠视为常规字符(原始字符串不就是这样吗?),但我可能忽略了一些明显的东西。
原因在这一节中用粗体标出的部分解释了:
String quotes can be escaped with a
backslash, but the backslash remains
in the string; for example, r"\"" is a
valid string literal consisting of two
characters: a backslash and a double
quote; r"\" is not a valid string
literal (even a raw string cannot end
in an odd number of backslashes).
Specifically, a raw string cannot end
in a single backslash (since the
backslash would escape the following
quote character). Note also that a
single backslash followed by a newline
is interpreted as those two characters
as part of the string, not as a line
continuation.
原始字符串不是100%原始的,仍然有一些基本的反斜杠处理。