Sometimes, while working with strings, you might face SyntaxError: EOL while scanning string literal error which occurs because of a number of reasons. The syntax errors are usually because of incorrectly writing the code. The syntax of the code is crucial in Python, so if there is any mistake, you will get the syntax error message. Solving such errors in Python is a great skill. In this short article, we will discuss how we can solve SyntaxError: EOL while scanning string literal errors using various methods. Moreover, we will also discuss how to understand errors in Python.
SyntaxError: EOL while scanning string literal
The SyntaxError: EOL while scanning string literal error occurs because of various reasons. The main reason is there must be an error in the syntax of the code. Syntax is very important in Python, so any miss type can cause syntax errors.
A syntax error is an error that occurs because of writing the code incorrectly. Depending on the reason for the error, there can be many possible solutions.
- If you have a string that has just opening quotation marks but not ending marks, then you need to add the closing quotation marks.
- If you have a string in multiple lines, or you have multiple line comments, then you need to use triple quotation marks.
- If you don’t want to use triple quotation marks for the multiple-line strings, then make sure you are using escape characters.
- Make sure to use proper and matching quotation marks. If you have started the string with double quotation marks, then make sure to end the string with double quotation marks as well.

Here we will go through the following methods to solve the problem:
- Ending quotation marks
- Multiple line comments
- Correctly adding multiple lines of comment
- Use matching quotation
Solution-1: Ending quotation marks
If you have a string that does not have the ending quotation marks then, you have to add the ending quotation marks. For example, you might have string something like this:
# string 1
string1 = 'This is incorrect way
# second string
string2 = "This is also incorrect
Note that there is an error because we started the string with quotation marks but then didn’t end the string with the same quotation marks.
In order to solve the error, we need to add the matching quotation marks at the end of the string as shown below:
# string 1
string1 = 'This is incorrect way'
# second string
string2 = "This is also incorrect"
As you can see, this time we have added the correct ending to the strings using appropriate quotation marks:
So, if you have a string in your code that does have starting quotation marks but does not have ending quotation marks, then you need to add appropriate quotation marks:
Solution-2: Multiple line comments
If you want to use multiple-line comments but have used single or double quotation marks, then you will again get the error. Or maybe you have started the multiple-line comment with triple quotation marks but forget to end the triple quotation marks.
One of the possible reasons for the error while using multiple comments can be:
# multiple line comments
''' This is
multiple line comment''
Notice that we have started the comment with triple quotation marks, but ended with double quotation marks. So, if you have multiple lines of comments, then make sure that you have used triple quotation marks as shown below:
# multiple line comments
''' This is
multiple line comment'''
Notice that we have used triple quotation marks to add multiple lines of comments in order to get rid of SyntaxError: EOL while scanning string literal.
Solution-3: Correctly add multiple lines of string
Another reason for getting SyntaxError: EOL while scanning string literal error is to have an incorrect string in multiple lines. For example, the following is the incorrect way of defining multiple lines of string:
# multipe line of string
string = " this is multiple
line of string"
As you can see, we have both double quotation marks at the beginning and end of the string but still are getting SyntaxError: EOL while scanning the string literal error.
If we want to have a string in multiple lines, then we should use escape characters as shown below:
# multipe line of string
string = "this is multiple \nline of string"
# printng the string
print(string)
Output:
this is multiple
line of string
As you can see, we have used an escape character to have a string in multiple lines:
Another way is to just use the escape character inside the string as shown below:
# multipe line of string
string = "this is multiple \
line of string"
This will not give the SyntaxError: EOL while scanning the string literal error anymore.
Solution-4: Use matching quotation marks
Sometimes you get SyntaxError: EOL while scanning the string literal error because of having unmatched quotation marks around a string. Let us say you have a string that has double quotation marks in the beginning and then you added single quotation marks at the end. So, make sure that you are using correct quotation marks at the end and beginning of the string.
Incorrect way:
# string 1
string1 = "string one'
# string2
string2 ='string"
The correct way is to use matching quotation marks:
# string 1
string1 = "string one"
# string2
string2 ='string'
This is the correct way of defining strings:
Understanding SyntaxError: EOL while scanning the string literal
In Python the syntax error is an error that occurs while the execution. When we miss typed any syntax in Python, we get a Syntax error.
Because syntax errors refer to issues by how your code is written, specifically, fixing these issues relates to fixing the syntax of your code. Thankfully, the traceback that Python returns about the error identifies where the issue can be found.
Specifically, the Python SyntaxError: EOL while scanning string literal refers to strings ending improperly at the end of the line of code.
Summary
In this short article, we discussed how we can solve SyntaxError: EOL while scanning the string literal. We came across five different methods to solve SyntaxError: EOL while scanning the string literal error.
Related issues:
- ValueError: Need More than 1 Value to Unpack
- TypeError: not enough arguments for format string
- SyntaxError: unexpected character after line continuation character