Syntax Error: EOL while scanning string literal

Syntax Error Eol String Featured Image

Python is an interpreted language, which essentially means that each line of code is executed one by one, rather than converting the entire program to a lower level code at once.

When the Python interpreter scans each line of code and finds something out of ordinary, it raises an error called the Syntax Error. These errors can be raised by “a missing bracket”, “a missing ending quote” and other basic anomalies in the syntax.

The Syntax Error we are going to discuss in this article is “EOL while scanning string literal”.

What does this error mean?

We can not solve a problem unless we effectively understand it. EOL stands for “End of Line”. The error means that the Python Interpreter reached the end of the line when it tried to scan the string literal.

The string literals (constants) must be enclosed in single and double quotation marks. Reaching the “end of line” while scanning refers to reaching the last character of the string and not encountering the ending quotation marks.

# String value
s = "This is a string literal...

# Printing the string 
print(s)

Running the above code gives the following output:

  File "EOL.py", line 2
    s = "This is a string literal...
                                   ^
SyntaxError: EOL while scanning string literal

The small arrow points the last character of the string indicating that the error occurred while parsing that component of the statement.

Now that we understand the problem, let us look at some instances where it can appear while running python code.


How to Fix “Syntax Error: EOL while scanning string literal”

There can be four main situations where this error can be encountered:

Missing the ending quotation mark

As explained in the above code snippet, Python interpreter raises a syntax error when it reaches the end of the string literal and finds that quotation mark is missing.

# Situation #1

# Missing the ending quotation mark
s = "This is a string literal...

# Printing the string 
print(s)

The reason of this syntax error is quite obvious. Every language has some basic syntax rules, which when violated lead to errors.

Solution:

The trivial solution is to respect the syntax rule and place the ending quotation marks.

# Solution #1

# Place the ending quotation mark
s = "This is a string literal..."

# Printing the string 
print(s)

Using the incorrect ending quotation mark

Python allows the use of ' ' and " " for enclosing string constants. Sometimes programmers use the incorrect quotation counterpart for ending the string value.

# Situation #2

# Incorrect ending quotation mark
s = "This is a string literal...'

# Printing the string 
print(s)

Even though the string appears to be enclosed, it is not the case. The Python interpreter searches for the matching quotation mark at the ending of the string.

Solution:

The basic solution is to match the beginning and the ending quotation marks.

#		Solution #2

# Match the quotation marks
s = "This is a string literal..."

# Printing the string 
print(s)

String constant stretching to multiple lines

Many novice Python programmers make the mistake of stretching statements to multiple lines. Python considers a new line as the end of the statement, unlike C++ and Java that consider ';' as the end of statements.

#		Situation #3

# String extending to multiple lines
s = "This is a string literal...
		  Going to the next line"

# Printing the string 
print(s)

At first, the code may seem ordinary, but as soon as the new line is started, the Python interpreter puts an end to that statement and raises an error for not enclosing the string constant.

Solution 1:

The escape sequence '\n' can be used to provide the effect of a new line to the string constant. Visit here to learn about other escape sequences.

#		Solution #3.1

# Using the escape sequences \n -> Newline
s = "This is a string literal... \n Going to the next line"

# Printing the string 
print(s)

Solution 2:

The other solution is to use triple quotation marks, ''' or """ for storing multi-line string literals.

#		Solution #3.2

# Using triple quotation marks 
s = """This is a string literal...
		  Going to the next line"""

# Printing the string 
print(s)

Using backslash before the ending quotation mark

The backslash '\' is responsible for escaping the string and causing syntax error.

#		Situation #4

# Storing a directory path 
s = "\home\User\Desktop\"

# Printing the string 
print(s)

The last backslash before the quotation mark escapes the string constant and Python interpreter considers \" as a single character. This escape sequence translates to a quotation mark (").

Solution:

The solution is to replace the backslash with an escape sequence for a backslash (\\).

#		Solution #4

# Storing a directory path 
s = "\\home\\User\\Desktop\\"

# Printing the string 
print(s)

Conclusion

A single error in a code spanning to a thousand lines can cost hours to debug. Therefore it is advised to write such codes with extreme concentration and using the correct syntax.

We hope this article was fruitful in solving the reader’s errors. Thank you for reading.