Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

SyntaxError EOL While Scanning String Literal

|| 0

SyntaxError EOL While Scanning String Literal
Stechies

What is Error EOL while scanning string literal?

EOL (End of Line) error in python occurs when while scanning a string of a program the python hit the end of the line due to the following reasons:

  • Missing Quotes for closing the string
  • String Extends Past one Line

1) Missing Quotes for closing the string

Example

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

2)  String Extends Past one Line

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

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

How to solve EOL while scanning string literal? 

1) Missing Closing Quotes of the string

Error Code

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

Output

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

Solution

Please make sure that string should always be closed with single or double quotes

Correct Code

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

Output

List is empty

2) String Extends Past one Line

Error Code

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

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

Output

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

Solution

As we know string normally cannot be extended to multiple lines, therefore, try to pass a whole string into a single line. 

Correct Code

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

Output

Tuple is empty

Note:  if you want the sting to be initialized in multiple lines then please use triple quotes (""" """) to expand your string like below

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

Output

Tuple is 
empty

 


Advertisement
Advertisement
Advertisement