Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: SyntaxError: EOL while scanning string literal

People also ask

How do I fix SyntaxError EOL while scanning string literal in Python?

The Python interpreter will return an EOL error if the quotation mark at the end of the string literal is missing. We can easily fix this problem by making sure the end quotation marks are in place. The characters '' and “ can be used to enclose string constants in Python.

What is the likely cause of an EOL error?

This error is commonly caused by: Strings that span multiple lines using the wrong syntax. Missing quotation marks. Mismatching quotation marks.

What does EOF while scanning triple quoted string literal mean in Python?

The SyntaxError: EOF while scanning triple-quoted string literal error occurs when you miss the triple-quote in the multiline string. The multiline string is enclosed with a triple quote. If the triple-quote is missed in the multiline string, the EOF (end of file) character is detected.

What is string literal in Python?

A string literal is where you specify the contents of a string in a program. >>> a = 'A string' Here 'A string' is a string literal. The variable a is a string variable, or, better put in Python, a variable that points to a string. String literals can use single or double quote delimiters.


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
"""

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.


(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.....\
...\
...."

In my situation, I had \r\n in my single-quoted dictionary strings. I replaced all instances of \r with \\r and \n with \\n and it fixed my issue, properly returning escaped line breaks in the eval'ed dict.

ast.literal_eval(my_str.replace('\r','\\r').replace('\n','\\n'))
  .....

I faced a similar problem. I had a string which contained path to a folder in Windows e.g. C:\Users\ The problem is that \ is an escape character and so in order to use it in strings you need to add one more \.

Incorrect: C:\Users\

Correct: C:\\\Users\\\