首页 > 解决方案 > 长字符串的 Python 外观转义

问题描述

我有一些愚蠢的东西让我很烦。使用 flake8 linter,他们显然喜欢短线,如果我运行如下字符串继续下一行:

print('\nWe interrupt this program to annoy you and make things \
          generally more irritating.\n')

这是打印的:

We interrupt this program to annoy you and make things               generally more irritating.

在打印输出之间没有大空间的情况下继续一行代码的正确方法是什么?

标签: pythonflake8

解决方案


这有效:

print("\nWe interrupt this program to annoy you and make things "
      "generally more irritating.\n")

你不需要任何继续。只需将两个字符串放在一起,中间没有任何内容,Python 解析器就会知道连接它们的意思

这称为字符串文字连接


推荐阅读