正如PEP8所建议的那样,在python程序中保持低于80列的规则,对于长字符串,我怎么能遵守这个规则呢?

s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."

我该如何把它扩展到下面一行呢?

s = "this is my really, really, really, really, really, really" + 
    "really long string that I'd like to shorten."

当前回答

您丢失了一个空格,您可能需要一个行延续字符,即。一个\。

s = "this is my really, really, really, really, really, really" +  \
    " really long string that I'd like to shorten."

甚至:

s = "this is my really, really, really, really, really, really"  \
    " really long string that I'd like to shorten."

parns也可以代替行续符,但可能会有人认为您打算使用元组,而只是忘记了逗号。举个例子:

s = ("this is my really, really, really, really, really, really"
    " really long string that I'd like to shorten.")

对比:

s = ("this is my really, really, really, really, really, really",
    " really long string that I'd like to shorten.")

使用Python的动态类型,代码可以以任何一种方式运行,但会产生不正确的结果。

其他回答

我以前用过textwrap.dedent。这有点麻烦,所以我现在更喜欢行延续,但如果你真的想要块缩进,我认为这很好。

示例代码(其中trim是去掉带有切片的第一个'\n'):

import textwrap as tw
x = """\
       This is a yet another test.
       This is only a test"""
print(tw.dedent(x))

解释:

Dedent根据新行前第一行文本中的空白计算缩进。如果你想调整它,你可以使用re模块轻松地重新实现它。

这种方法有局限性,很长的行可能仍然比你想要的长,在这种情况下,其他方法连接字符串更适合。

隐式连接可能是最干净的解决方案:

s = "this is my really, really, really, really, really, really," \
    " really long string that I'd like to shorten."

编辑经过反思,我同意托德的建议,使用括号而不是行接续更好,因为他给出了所有的理由。我唯一的犹豫是,用括号括起来的字符串和元组比较容易混淆。

您丢失了一个空格,您可能需要一个行延续字符,即。一个\。

s = "this is my really, really, really, really, really, really" +  \
    " really long string that I'd like to shorten."

甚至:

s = "this is my really, really, really, really, really, really"  \
    " really long string that I'd like to shorten."

parns也可以代替行续符,但可能会有人认为您打算使用元组,而只是忘记了逗号。举个例子:

s = ("this is my really, really, really, really, really, really"
    " really long string that I'd like to shorten.")

对比:

s = ("this is my really, really, really, really, really, really",
    " really long string that I'd like to shorten.")

使用Python的动态类型,代码可以以任何一种方式运行,但会产生不正确的结果。

因为相邻的字符串常量是自动连接的,你可以这样编码:

s = ("this is my really, really, really, really, really, really, "  
     "really long string that I'd like to shorten.")

注意没有加号,我在示例的格式后面添加了额外的逗号和空格。

就我个人而言,我不喜欢反斜杠,我记得在某个地方读到过,它的使用实际上是被反对的,而支持这种更明确的形式。记住“显式优于隐式”。

我认为反斜杠不太清楚,也不太有用,因为这实际上是转义换行符。如果需要的话,不能在它后面加上行结束注释。可以通过串接字符串常量来实现:

s = ("this is my really, really, really, really, really, really, " # comments ok
     "really long string that I'd like to shorten.")

我使用谷歌搜索“python行长”,它返回PEP8链接作为第一个结果,但也链接到另一个关于这个主题的StackOverflow帖子:“为什么python PEP-8应该指定79个字符的最大行长?”

另一个好的搜索短语是“python line continuation”。

可用的选项:

反斜杠:"foo" \ "bar" "foo" + " \ "bar" 括号: (“foo”“酒吧”) 带加号的括号:("foo" + "bar") PEP8, E502:括号之间的反斜杠是多余的

避免

避免使用逗号:("foo", "bar")来定义一个元组。


>>> s = "a" \
... "b"
>>> s
'ab'
>>> type(s)
<class 'str'>
>>> s = "a" + \
... "b"
>>> s
'ab'
>>> type(s)
<class 'str'>
>>> s = ("a"
... "b")
>>> type(s)
<class 'str'>
>>> print(s)
ab
>>> s = ("a",
... "b")
>>> type(s)
<class 'tuple'>
>>> s = ("a" + 
... "b")
>>> type(s)
<class 'str'>
>>> print(s)
ab
>>>