50

I have a really long string in python:

long_string = '
this is a really
really
really
long
string
'

However, since the string spans multiple lines, python doesn't recognize this as a string. How do I fix this?

SuperString
  • 19,925
  • 36
  • 90
  • 117

4 Answers4

109

You can also do this, which is nice because you have better control over the whitespace inside of the string:

long_string = (
    'Lorem ipsum dolor sit amet, consectetur adipisicing elit, '
    'sed do eiusmod tempor incididunt ut labore et dolore magna '
    'aliqua. Ut enim ad minim veniam, quis nostrud exercitation '
    'ullamco laboris nisi ut aliquip ex ea commodo consequat. '
    'Duis aute irure dolor in reprehenderit in voluptate velit '
    'esse cillum dolore eu fugiat nulla pariatur. Excepteur sint '
    'occaecat cupidatat non proident, sunt in culpa qui officia '
    'deserunt mollit anim id est laborum.'
)
FogleBird
  • 70,488
  • 25
  • 121
  • 130
  • 5
    Indeed, this is one of the things that I ran into when I wanted to create a tuple with one element ;) – plaes Dec 20 '11 at 16:20
  • This appears to not work the moment you need any evaluated value that isn't a string. Typically for me, when using list comprehension to implode complex data into a string; and I have not found a way to combine such an expression with this string definition style. :( – ThorSummoner Nov 25 '15 at 19:58
  • You can also use long_string = textwrap.dedent('''long string with each line indented''') https://docs.python.org/3/library/textwrap.html#textwrap.dedent – moorecm Dec 01 '15 at 19:33
  • 2
    FYI: This is the preferred way of declaring a [long string in multiple lines.](https://www.python.org/dev/peps/pep-0008/#maximum-line-length) and the accepted answer will introduce next line characters(which are not always needed.) – Rohit Rawat Jan 31 '20 at 15:25
58
long_string = '''
this is a really
really
really
long
string
'''

""" does the same thing.

nmichaels
  • 47,648
  • 12
  • 99
  • 131
  • 5
    In cases where indenting the string's lines makes the code more readable, you can use `dedent` to remove the indentation in the resulting string. – IanS Aug 30 '16 at 10:11
  • 1
    For me this is adding \n \n characters into the string when printed. How would I avoid this ?? – blissweb Apr 19 '20 at 08:40
27

You can use either

long_string = 'fooo' \
'this is really long' \
'string'

or if you need linebreaks

long_string_that_has_linebreaks = '''foo
this is really long
'''
plaes
  • 30,266
  • 10
  • 85
  • 86
  • 6
    Also, for your first option if you have parentheses round the string you don't need the backslashes. However notice the big drawback with using string concatenation: unless you are really careful about spaces you could end up with `"fooothis is really longstring"` which probably wasn't what you wanted. – Duncan Dec 20 '11 at 16:00
4

I was also able to make it work like this.

long_string = '\
this is a really \
really \
really \
long \
string\
'

I can't find any online references to this way of constructing a multi-line string. I don't know if it's correct. My suspicion is that python is ignoring the newline because of the backslash? Maybe someone can shed light on this.

Shun Y.
  • 73
  • 6