Python: SyntaxError: EOL while scanning string literal

Problem

I have the above-mentioned error in s1="some very long string............"

Does anyone know what I am doing wrong?

Solution 1

You are not putting a ” before the end of the line.

Use “”” if you want to do this:


""" a very long string ......
....that can span multiple lines
"""

And careful to only use three quotation marks, I had four by accident and the error came you.

 

Solution 2

I had this problem – I eventually worked out that the reason was that I’d included \ characters in the string. If you have any of these, “escape” them with \\ and it should work fine.

Solution 3

(Assuming you don’t have/want line breaks in your string…)

How long is this string really?

I suspect there is a limit to how long a line read from a file or from the commandline can be, and because the end of the line gets choped off the parser sees something like s1="some very long string.......... (without an ending ") and thus throws a parsing error?

You can split long lines up in multiple lines by escaping linebreaks in your source like this:

s1="some very long string.....\
...\
...."