[FIXED] SyntaxError: EOL while scanning string literal

Rate this post

Problem: How to fix “SyntaxError: EOL while scanning string literal” in Python?

Why does SyntaxError: EOL while scanning string literal Occur?

Python is a dynamically typed and interpreted programming language, which means that each line of code gets executed one by one, and also, you don’t have to declare the type of a variable as it is automatically chosen. Python has a robust error handling mechanism that helps you to catch many different errors as soon as something fishy happens in a particular line of the code. The SyntaxError: EOL while scanning string literal is one such error that occurs while parsing strings in Python.

So, what is EOL? The term EOL is an abbreviation for End of Line that implies that the coding process terminates as it reached the end of line before reaching the correct syntax. In other words, an EOL ( End of Line ) error occurs when the interpreter expects a particular character/set of characters to have appeared in a particular line of code; however, the desired characters were not found before the end of the line. Thus, Python stops the program execution and throws a syntax error.

Example:

def message():
    msg = "Hello and Welcome to Finxter
    return msg
print(message())

Output:

  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\SyntaxError-EOL.py", line 2
    msg = "Hello and Welcome to Finxter
                                       ^
SyntaxError: EOL while scanning string literal

Explanation: The above error occurs as it has an incomplete syntax (incomplete quotes(“)). While returning the print message, we have not used the closing double-quotes. (“).

Now that you know the reason behind the occurrence of "SyntaxError: EOL while scanning string literal", let’s have a look at different scenarios leading to this syntax error and the ways to eliminate them.

Case 1: Closing The Quotes

We clearly saw in the above example that the error occurred because of incomplete quotes (wrong syntax). It can be solved by simply closing the quotes while returning the string output. You just have to search for the string declarations in the code and then check if the string has incomplete end quotes. It can also be enclosed in groups of three double quotes or single quotes.

Solution:

def message():
    # close the quotes to eliminate the syntax error
    msg = "Hello and Welcome to Finxter"
    return msg


print(message())

Output:

Hello and Welcome to Finxter

Note:  Strings can also be enclosed in groups of three, double or single quotes as shown below.

def message():
    # close the quotes to eliminate the syntax error
    str_1 = 'String within single quotes!'
    str_2 = "String within double quotes!"
    str_3 = """This is a String within triple quotes.
Generally, spanning strings over multiple lines can be done using
Python's Triple Quotes as shown here."""

    return [str_1, str_2, str_3]


for i in message():
    print(i)
    print("=======================")

Output:

String within single quotes!
=======================
String within double quotes!
=======================
This is a String within triple quotes.
Generally, spanning strings over multiple lines can be done using
Python's Triple Quotes as shown here.
=======================

Case 2: Additional Quotes

We just saw that strings can be enclosed within single, double or triple quotes. But it is essential to understand that, the type of quote (single quote/double quote/triple quote) used to open a string ought to be the same as the type of quote used to close a string. Otherwise, "SyntaxError: EOL while scanning string literal" is returned when the quotes used to open and close the string are not the same. Hence it is necessary to check whether the string has any additional quotes. 

Example: Let’s have a look at an example that demonstrates the occurrence of the error because wrong quotes were used to open and close the string value.

def message():
# Error as different quotes were used to enclose the string value 
    s = 'Hello and Welcome to Finxter"
    return s
print(message())

Output:

  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\SyntaxError-EOL.py", line 3
    s = 'Hello and Welcome to Finxter"
                                      ^
SyntaxError: EOL while scanning string literal

Explanation: The above error occurred because the string value had a single quote as the opening quote but a double quote as the closing quote.

Solution: To avoid this error, you just have to locate the string and use the same quote for opening and closing the string value. In this case, single quote was used as an opening quote for the string, so we will be using a single quote to close it as well.

def message():
    # Error as different quotes were used to enclose the string value
    s = 'Hello and Welcome to Finxter'
    return s


print(message())

# Hello and Welcome to Finxter

Case 3: Spanning Multiple Lines

In Python, a multiline string starts and ends with either three single quotes (”’) or three double quotes (” ” “). Apart from this, any kind of quotes, tabs, or newlines in the middle of these quotes are viewed as a part of the string. Also, the indentation rules for blocks in Python do not make any difference to lines inside a multiline string.

However, if you try to enclose multiline strings within single or double quotes you will encounter a syntax error as shown below.

Example:

def message():
# Multiple line-string
    msg = "This is line 1.
        This is line 2.
        This is line 3."
    return msg
    
print(message())

Output:

  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\SyntaxError-EOL.py", line 3
    msg = "This is line 1.
                          ^
SyntaxError: EOL while scanning string literal

Solution 1: The easiest way to solve this error is to simply use triple quotes in order to enclose the multiline text.

def message():
    # Multiple line-string
    msg = '''This is line 1.
This is line 2.
This is line 3.'''
    return msg


print(message())

Output:

This is line 1.
This is line 2.
This is line 3.

Solution 2: In case you do not wish to use triple quotes then you can use escape sequences.  The escape sequence '\n' is used inside the string that helps to provide the effect of a new line.

def message():
    # Multiple line-string
    msg = 'This is line 1.\nThis is line 2.\nThis is line 3.'
    return msg


print(message())

Output:

This is line 1.
This is line 2.
This is line 3.

Note: If you have a huge string and you do not wish to print it in different lines, instead you simply want to place the string in different lines for better visibility of your code in the display screen then you can put each line in numerous lines by putting a backslash between them as shown below.

Example:

def message():
    # Multiple line-string
    header = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2)" \
          " AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9"
    return header


print(message())

Output:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9

Case 4: Backslash Before Ending Quotes

In some cases, the occurrence of backslash '\'  before the ending quotes of a string can be the reason that can cause the syntax error. The backslash before the quote escapes the string constant and hence the interpreter in Python considers it as a single character. Generally, this escape sequence translates to a quote (“).

Example: Let’s have a look at an example to understand this.

def message():
# String with backlash before end quote
    path = "C:\data\db\diagnostic.data\"
    return path


print(message())

Output:

  File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxer\SyntaxError-EOL.py", line 3
    path = "C:\data\db\diagnostic.data\"
                                        ^
SyntaxError: EOL while scanning string literal

Solution: To solve this error, you have to only replace the single backlash ‘\’  with the escape sequence for the backslash (\\).

def message():
# String with backlash before end quote
    path = "C:\data\db\diagnostic.data\\"
    return path


print(message())

Output:

C:\data\db\diagnostic.data\

We have come to the end of our discussion here. I hope this tutorial helped you. Please stay tuned and subscribe for more interesting articles and discussions.

Article contributed by: Shubham Sayon and Rashi Agarwal


To become a PyCharm master, check out our full course on the Finxter Computer Science Academy available for free for all Finxter Premium Members: