Gangmax Blog

自由之思想,独立之精神

How to Write Long String That Conforms With PEP8

| Comments

From here.

Also, because neighboring string constants are automatically concatenated, you can code it like this too:

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

Note no plus sign, and I added the extra comma and space that follows the formatting of your example.

Personally I don’t like the backslashes, and I recall reading somewhere that its use is actually deprecated in favor of this form which is more explicit. Remember “Explicit is better than implicit.”

I consider the backslash to be less clear and less useful because this is actually escaping the newline character. It’s not possible to put a line end comment after it if one should be necessary. It is possible to do this with concatenated string constants:

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

Comments