6

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

fredrik
  • 9,631
  • 16
  • 72
  • 132
  • 1
    Usually if you end up with texts of 10 lines, it does not belong in your python code. – spectras Oct 28 '16 at 15:06
  • 2
    There's also `textwrap.dedent` to strip leading whitespace from your second example, but I think this is a pretty opinion-based question. The `dedent` solution probably looks the cleanest and is most resilient to changing the content, but it imposes a run-time cost. I would use the third example, personally. – chepner Oct 28 '16 at 15:06
  • Possible duplicate of [A simple way to remove multiple spaces in a string in Python](http://stackoverflow.com/questions/1546226/a-simple-way-to-remove-multiple-spaces-in-a-string-in-python) – cxw Oct 28 '16 at 15:09
  • FWIW, Django [follow the `tedious()` approach](https://github.com/django/django/blob/3c447b108ac70757001171f7a4791f493880bf5b/django/utils/lorem_ipsum.py) – brianpck Oct 28 '16 at 15:11
  • 1
    BTW, what's the purpose of your `replace` function in `hello()`? `split()` already removes all the line breaks. – brianpck Oct 28 '16 at 15:14
  • @brianpck ah, you are so right. That was simply a mistake on my part. I'll remove the `replace` function. – fredrik Oct 28 '16 at 15:17
  • 1
    I've been using the `tedious` solution for the past 15+ years and I'm fine with it. – bruno desthuilliers Oct 28 '16 at 15:17
  • I don't know that you're going to find a *consensus*. I presume you meant that and not *census*? – Mark Ransom Oct 28 '16 at 15:21
  • @MarkRansom yes, edited. Thanks. No, it seems I'm not getting a consensus ;) – fredrik Oct 28 '16 at 15:24
  • 2
    There's also a strong case to be made for such strings being defined as global constants, defined at module scope where indentation not an issue. – chepner Oct 28 '16 at 15:28
  • @chepner thanks so much for the suggestion on `textwrap.dedent`. This is my new go-to solution. I just recently saw this too: https://amir.rachum.com/blog/2018/06/23/python-multiline-idioms/ – fredrik Jul 05 '18 at 06:54

0 Answers0