The resulting string has a linebreak (and I believe this syntax breaks function folding in my IDE):
>>> def linebreak():
>>> return """Hello this is a really long string which eventually
>>> needs to be breaked due to line length"""
>>> print(linebreak())
Hello this is a really long string which eventually
needs to be breaked
This is nicer, but this not only includes the line break but also includes numerous spaces in the resulting string:
>>> def whitespace():
>>> some_string = """Hello this is a really long string which eventually
>>> needs to be breaked due to line length"""
>>> print(whitespace())
Hello this is a really long string which eventually
needs to be breaked
As suggested in this SO Q/A, you have to carefully format each line (which is very tedious) to make sure you end or start with a space. If you happen to edit the string after you initially created it, you also may end up with lines of text with very odd line lengths (imagine 10 lines of text of very different length):
>>> def tedious():
>>> return ('Hello this is a really long string which eventually '
>>> 'needs to be breaked due to line length')
>>> print(tedious())
Hello this is a really long string which eventually needs to be breaked due to line length
What's the common consensus on how to define long strings with PEP-8 in mind and without getting linebreaks or whitespace explicitly where I define them?
This is super convoluted but kind of does what I want although it makes it impossible to explicitly define a linebreak:
>>> def hello():
>>> return ' '.join("""Hello this is a really long string which
>>> eventually needs to be breaked due to line
>>> length""".split())
>>> print(hello())
>>> Hello this is a really long string which eventually needs to be breaked due to line length
Question: Any suggestions on improving this last convoluted approach?
Update
Since this question was [closed] I would just like to add that I think textwrap.dedent
is the very best solution (for me) as described here. There is also another question (which did not get closed) which has many other suggestions: Pythonic way to create a long multi-line string