How to declare a long string in Python?

Question:

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?

Asked By: SuperString

||

Answers:

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

""" does the same thing.

Answered By: nmichaels

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
'''
Answered By: plaes

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.'
)
Answered By: FogleBird

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.

Answered By: Shun Y.

This is normally a comment but indentation does not seem to work in comments.

In the code below I show how the answer by @ShunYu. , which seems quick to type, compares with the solution by @Superstring in the context of a function definition:

def test() :


    string1='
The city was bustling with activity as 
people hurried to and fro, going about 
their daily routines.
n
Cars honked their horns, buses roared down the streets, and the air was filled with the sound of chatter and laughter.
'

    string2=(
    'The city was bustling with activity as '
    'people hurried to and fro, going about '
    'their daily routines.'
    'n'
    'Cars honked their horns, buses roared down the streets, and the air was filled with the sound of chatter and laughter.'
    )
    print(string1)
    print('n')
    print(string2)

Output:

The city was bustling with activity as people hurried to and fro,
going about their daily routines. Cars honked their horns, buses
roared down the streets, and the air was filled with the sound of
chatter and laughter.

The city was bustling with activity as people hurried to and fro,
going about their daily routines. Cars honked their horns, buses
roared down the streets, and the air was filled with the sound of
chatter and laughter.

The answer provided by @SuperString allows the string to be indented inside a function whereas the answer by @ShunYu. seems to require removing the indentation..

Answered By: userrandrand
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.