Python错误-或者我的愚蠢-扫描字符串li时下线

2024-05-14 15:28:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我看不出下面这两行有什么显著的区别。

然而,第一次解析和第二次解析都没有。

In [5]: n=""" \\"Axis of Awesome\\" """

In [6]: n="""\\"Axis of Awesome\\""""
  File "<ipython-input-6-d691e511a27b>", line 1
    n="""\\"Axis of Awesome\\""""
                                ^
SyntaxError: EOL while scanning string literal

这是一个Python bug/feature/odity,还是我遗漏了一些基本的东西?


Tags: ofininputstringipythonlinefileawesome
2条回答

您的最后4个引号将被计算为"" & "",而不是您期望的" & """

中的最后四个引号

"""\\"Axis of Awesome\\""""

被解析为""",即字符串结束,然后是",即新字符串文本的开始。不过,这个新的文字从来没有完成过。简单示例:

>>> """foo""""bar"
'foobar'
>>> """foo""" "bar"
'foobar'

如果要避免此问题,请将"""替换为r',或者退出"

>>> """\\"Axis of Awesome\\\""""
'\\"Axis of Awesome\\"'
>>> r'\"Axis of Awesome\"'
'\\"Axis of Awesome\\"'

相关问题 更多 >

    热门问题