Register Login

SyntaxError EOL While Scanning String Literal

30 May 2019 9:35 am || 0

SyntaxError EOL While Scanning String Literal

In this article, we will learn about the error SyntaxError EOL while scanning String literal.

SyntaxError are detected before the programs runs. And is usually a typing mistake or syntax mistakes.

This SyntaxError occurs while the interpreter scans the string literals. And it hits the EOL(‘End of Line’). But did not find a specific character before the EOL.

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

What is Error EOL while scanning string literal?

EOL (End of Line) error occurs in python while scanning a string the python interpreter hits the end of the line. And the reasons being.

1. Missing Quotes for Closing the String:

While closing our string, there are often times when we forget to add an inverted comma (single or double) to our string. When this happens the python interpreter is not able to find the End of the line while scanning the string. Thus the error, SyntaxError EOL while scanning the string literal 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 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 not able to find the end of the string. Thus the 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”.  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 encountered.

Solution

As we know we can not expand string in multiple lines, thus, try to pass the whole string into 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