Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a long string in Python?

Tags:

python

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?

like image 518
SuperString Avatar asked Dec 20 '11 14:12

SuperString


People also ask

How do you make a long string in Python?

Use a backslash ( \ ) as a line continuation character In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.

How do you declare a string in Python?

How to create a string in Python? Strings can be created by enclosing characters inside a single quote or double-quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.

How do you return a multiline string in Python?

Use """ (double quote) or ''' (single quote) for multiline string or use \ for seperating them. NOTE: """ and ''' should be used with care otherwise it may print too many spaces in between two lines.


1 Answers

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.' ) 
like image 143
FogleBird Avatar answered Sep 20 '22 02:09

FogleBird