5

What is the Pythonic way of writing a single-line but long string in program:

s = 'This is a long long string.'

Additionally, the string may need to be formatted with variables:

s = 'This is a {} long long string.'.format('formatted')

Existing Solution 1

s = 'This is a long '\
        'long '\
        'string.'

Additional trailing \ characters make reformatting very difficult. Joining two lines with a \ gives an error.

Existing Solution 2

s = 'This is a long \
long \
string.'

Except for a similar problem as above, subsequent lines must be aligned at the very beginning, which gives awkward readability when the first line is indented.

Cyker
  • 9,946
  • 8
  • 65
  • 93
  • 4
    Not a duplicate.. this appears to be about single line long strings, while that stackoverflow question is for multi-line strings. – rgilligan Nov 28 '16 at 03:33
  • @chris I don't think this is a duplicate to the other question being referenced. This is about single-line strings, not multi-line ones. – rgilligan Nov 28 '16 at 03:45
  • Agree with @rgilligan – the question being referred to is for *multi-line* strings, please re-open this question. – Jerrybibo Nov 28 '16 at 03:48
  • 1
    I actually voted to close this as primarily opinion-based, not as a duplicate of that question. I agree that it is different. However, this _has_ been asked before, e.g. https://stackoverflow.com/questions/2058925/how-can-i-break-up-this-long-line-in-python. It probably shouldn't be reopened. – ChrisGPT was on strike Nov 28 '16 at 03:50

2 Answers2

9

For long strings where you don't want \n characters, use 'string literal concatenation':

s = (
    'this '
    'is '
    'a '
    'long '
    'string')

Output:

This is a long string

And it can be formatted as well:

s = (
    'this '
    'is '
    'a '
    '{} long '
    'string').format('formatted')

Output:

This is a formatted long string

rgilligan
  • 754
  • 5
  • 18
  • 2
    [Reference](https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation). – ChrisGPT was on strike Nov 28 '16 at 03:33
  • This one looks the best at the moment, but it doesn't work with formatting command in text editors such as vim's `gq` when you modify the string. Also, you may need to write multiple `.format`, one for each element you need to format. – Cyker Nov 28 '16 at 03:34
  • 2
    @Cyker: That is incorrect, you only write one `.format()`. – Dietrich Epp Nov 28 '16 at 03:37
  • @rgilligan All right. It works and by giving it a second look I now understand why. This looks cool. Thank you very much. – Cyker Nov 28 '16 at 03:50
  • @rgilligan However, I want to point that you need trailing spaces in each string element as Python doesn't interpolate them automatically. – Cyker Nov 28 '16 at 03:52
  • @DietrichEpp That's true. In the very end of the string. Thank you a lot. – Cyker Nov 28 '16 at 03:53
  • @Cyker thanks, I updated the answer to reflect those things. – rgilligan Nov 28 '16 at 03:59
1

Here's the PEP8 guideline: https://www.python.org/dev/peps/pep-0008/#maximum-line-length

Wrap long lines in parenthesis.

Use a maximum of 72 characters per line for long lines of text.

If you have any operators in your string, place the line breaks before them.

Other than that, as long as you're not obscuring what's going on, it's pretty much up to you on how you want to do it.

Denny
  • 744
  • 4
  • 10