Register Login

SyntaxError EOL While Scanning String Literal

Updated Dec 09, 2020

SyntaxError EOL While Scanning String Literal?

Syntax errors are detected before the programs runs. Usually, it is just a typing mistake or a syntactical mistake. Such an error in Python is the SyntaxError EOL while scanning String literal. 

This SyntaxError occurs while the interpreter scans the string literals and hits the EOL(‘End of Line’). But if it does not find a specific character before the EOL, the error is raised.

Let us understand it more with the help of an example.

What is “SyntaxError EOL while scanning string literal”?

A SyntaxError EOL(End of Line) error occurs when the Python interpreter does not find a particular character or a set of characters before the line of code ends. When the error is raised, the code execution is halted.  

1. Missing Quotes for Closing the String:

While closing a string, there are often times when we forget to add an inverted comma (single or double). When this happens, the Python interpreter is not able to find the End of the line while scanning the string. Thus the SyntaxError EOL error occurs. 

Example 1:

MyList = []
if not MyList:
          print("MyList is empty
else:
        print("MyList is not empty")

Output:

File "main.py", line 3
    print("MyList is empty
                       ^
SyntaxError: EOL while scanning string literal

Explanation

In the above code, we have initialized an empty list MyList and used an if-else block to print if ‘MyList’ is empty or not. Inside the if block the print statement is used to print a string. But the string is missing double inverted commas at the end. And because of the missing commas, the Python interpreter is unable to find the end of the string. 

Thus the SyntaxError error is encountered.

SyntaxError EOL While Scanning String Literal

Solution

Make sure that string should always be closed within single or double-quotes.

Correct Code

llist = []
if not llist:
        print("List is empty")
else:
        print("List is not empty")

Output

MyList is empty

2. String Extends Past one Line

In Python, we can not extend our string which is enclosed within a single or double inverted comma past a single line. If we try to do so the error “SyntaxError EOL while scanning the string literal occurs” will pop up.  If we want our string to extend in multiple lines, then they should be enclosed within triple inverted commas (single or double).

Example 2:

ttuple = ()
if not ttuple:
        print("Tuple is

empty")
else:
        print("Tuple is not empty")

Output :

 file "main.py", line 3
    print("MyTuple is 
                   ^
SyntaxError: EOL while scanning string literal

Explanation

In the above code, we have initialized an empty tuple ‘MyTuple’ and used if-else block to print if ‘MyTuple’ is empty or not. Inside the if block the print statement is used to print a string. But the string is expanded in multiple lines. And is not interpreted by the python interpreter. Thus the error is raised.

Solution

Try to keep the entire string within a single line.  

Correct Code:

MyTuple = ()
if not MyTuple:
        print("MyTuple is empty")
else:
        print("MyTuple is not empty")

Output:

MyTuple is empty

Note:  If you want the string to be initialized in multiple lines. Then use triple inverted commas either single(''' Single quotes ''') or double("""Double quotes """") to enclose your string.

Example:

MyTuple = ()
if not MyTuple:
        print("""MyTuple is

        empty""")
else:
        print("MyTuple is not empty")

Output:

MyTuple is
empty

Conclusion

We hope all the scenarios explained above will help you prevent the SyntaxError EOL while scanning String literal error. Another mistake you must avoid is using mismatched quotations. While closing strings, make sure that if it begins with single quotes, it must end with double quotes.


×