如何编写符合PEP8的非常长的字符串并防止E501?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (61)

由于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.")

请注意没有加号,并且在示例的格式之后添加了额外的逗号和空格。

扫码关注云+社区