Search code examples
pythonstringpep8

How to write very long string that conforms with PEP8 and prevent E501


PEP8 suggests a line limit of 80 characters. But my string goes over the limit:

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

How should I format that to stay within the character limit? e.g.,

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

Solution

  • Implicit concatenation might be the cleanest solution:

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

    Edit On reflection I agree that Todd's suggestion to use brackets rather than line continuation is better for all the reasons he gives. The only hesitation I have is that it's relatively easy to confuse bracketed strings with tuples.