DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

Python syntaxerror: eol while scanning string literal

ItsMyCode |

Python is an interpreted language that means, unlike other languages( Java, C++, C#, etc. ), Python doesn’t convert the entire code into low-level machine code at once; rather, each line of code is executed line by line.

The Syntax Error mainly occurs when the Python interpreter finds some anomalies in your code. Some of the common issues are “ missing out the brackets ,” “missing quotes ,” “ if-else mismatch ,” etc.

What does EOL while scanning string literal error mean in Python?

EOL stands for “ End of line ,” and the Syntax error means that we have not followed the guidelines of Python Programming.

If you read the error carefully, Python is expecting a character at the end of the line. In this instance, Python expects the string closing character ( " ), which needs to be closed at the end of the line.

The eol while scanning string literal error in Python occurs in 3 main use cases.

  1. Missing quotation marks
  2. String spanning multiple lines using the wrong syntax
  3. Mismatch of quotes
  4. Using backslash before the ending quotation mark

Missing quotation marks

String in Python must be opened and closed properly with quotations marks; else, you will get a syntax error. Let’s take an example of a string that is not closed.

def getName():
    print("My name is Chandler Bing)
getName()

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 2
    return "My name is Chandler Bing
                                    ^
SyntaxError: EOL while scanning string literal
Enter fullscreen mode Exit fullscreen mode

Solution – We can easily fix this error by closing the string with the same quotation mark we used to open our string in the beginning.

def getName():
    print("My name is Chandler Bing")
getName()

# Output
My name is Chandler Bing
Enter fullscreen mode Exit fullscreen mode

String spanning multiple lines using the wrong syntax

In some cases, the string value can span multiple lines if you don’t use a proper syntax in the multi-line string, then Python will throw eol while scanning string literal error.

def getMessage():
    message= "This is Chandler Bing and i am one of the famous actor in the 
    TV Series called Friends. Welcome to My world"
    print(message)

getMessage()

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 2
    message= "This is Chandler Bing and i am one of the famous actor in the 
                                                                            ^
SyntaxError: EOL while scanning string literal
Enter fullscreen mode Exit fullscreen mode

*Solution – * If you have a multi-line string in Python, the best way to declare it is by enclosing it using triple quotes.

Either enclose it by using 3 single quotation*(''' Hello ''')* marks or 3 double quotation (""" Hello """) marks to resolve the issue.

Alternatively, you can declare the string in one line and use \n to split wherever required into multi-line.

def getMessage():
    message= """This is Chandler Bing and i am one of the famous actor in the 
    TV Series called Friends. Welcome to My world"""
    print(message)

getMessage()

# Output
This is Chandler Bing and i am one of the famous actor in the 
    TV Series called Friends. Welcome to My world
Enter fullscreen mode Exit fullscreen mode

Mismatch of quotes

The type of quote used to open a string should be the same as closing the string that means if you have used single quotes (') to open a string, do use single quotes (') to close a string.

def getMessage():
    message= "Hello World'
    print(message)

getMessage()

# Output
 File "c:\Projects\Tryouts\listindexerror.py", line 2
    message= "Hello World'
                          ^
SyntaxError: EOL while scanning string literal
Enter fullscreen mode Exit fullscreen mode

Solution – Changing the quotation mark*(")* to match the beginning of the string will resolve the issue here.

def getMessage():
    message= "Hello World"
    print(message)
getMessage()

# Output
Hello World

Enter fullscreen mode Exit fullscreen mode

Using backslash before the ending quotation mark

In storing the directory path in a variable, we often use backlash, which will lead to eol while scanning string literal error.

It causes a Syntax error because Python interprets backslash*(\)* as an escape sequence. So, in this case, it will treat both (\")as a single character, and according to Python, the string is not enclosed properly.

# Storing a directory path 
folderPath= "C:\Program Files\Python\"
print(folderPath)

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 2
    folderPath= "C:\Program Files\Python\"
                                          ^
SyntaxError: EOL while scanning string literal
Enter fullscreen mode Exit fullscreen mode

*Solution- * Replace the backslash with an escape sequence, adding double backslash so that Python can interpret and execute properly.

# Storing a directory path 
folderPath= "C:\\Program Files\\Python\\"
print(folderPath)

# Output
C:\Program Files\Python\
Enter fullscreen mode Exit fullscreen mode

The post Python syntaxerror: eol while scanning string literal appeared first on ItsMyCode.

Top comments (2)

Collapse
 
shivangchauhan7 profile image
Shivang Chauhan

Hey, could you please connect with me on LinkedIn as i cannot find you on there? linkedin.com/in/shivang-chauhan-57...

i read your post here - support.google.com/webmasters/thre...

I am having same issues, have you found the solution to this?

Collapse
 
srinivasr profile image
Srinivas Ramakrishna

Yes, first index your website and after a week publish in other medium. Since new sites have low authority google prefers ranking the higher authority sites.