SyntaxError: EOL while scanning string literal

The SyntaxError: EOL while scanning string literal error occurs when the Python Interpreter reaches the end of the line while scanning the string literal. EOL is an abbreviation for “End of Line”. The string must be surrounded by single and double quotation marks. The python interpreter reaches the last character of the line while scanning the string without encountering the closing quotation marks. To fix this error SyntaxError: EOL while scanning string literal, the single and double quotation marks should be used to enclose the string.

The SyntaxError: EOL while scanning literal string error is due to missing quotation in the string, typing mistakes generally in python. If the quote is missed, the string reaches EOL (End Of Line) while scanning the literal string. The python interpreter reaches the end of the line while scanning for a string literal or a character within the line. Therefore, the SyntaxError: EOL while scanning literal string error is thrown.

An EOL error is a syntax error that the Python interpreter expects a specific character or string to have occurred in a particular line of code, but the character or string would not have been found before the end of the line. It’s usually a typing error or syntax mistakes.



Exception

Python interpreter throws this syntax error “eol while scanning literal string” for this reason and exits the execution of the program. The syntax error is shown as below

  File "/Users/python/Desktop/test.py", line 1
    x = "My first Program
                   ^
SyntaxError: EOL while scanning string literal
[Finished in 0.1s with exit code 1]


Root Cause

In Python, the “SyntaxError: EOL while scanning the literal string” error occurs while scanning the string in the code line, and the python interpreter reaches the end of the line because of the reasons below.

  • Missing quotations to close the string
  • Mixing the quotations
  • Multiline String


Solution 1 – Missing Quotes

The reason for the syntax error is that the double quote in the string is missing. The double quote is missed either at the beginning of the string or at the end of the string. Identify the beginning and the end of the text, and enclose the text with a double quote. Fixing the missing quotation will resolve the syntax error “SyntaxError: EOL while scanning string literal”.

Program

x = "My first Program
print(x)

Error

  File "/Users/python/Desktop/test.py", line 1
    x = "My first Program
                   ^
SyntaxError: EOL while scanning string literal
[Finished in 0.1s with exit code 1]

Solution

x = "My first Program"
print(x)

OR

x = 'My first Program'
print(x)

Output

My first Program


Solution 2 – Mixing the Quotes

The reason for the syntax error is that the string uses a combination of a single quotation and a double quotation. The different quotation is used at string beginning and at string end. Identify the beginning and end of the text, and include the text with double quotation. Fixing the proper quotation will solve the error in the syntax.

Program

x = "My first Program'
print(x)

Error

  File "/Users/python/Desktop/test.py", line 1
    x = "My first Program
                   ^
SyntaxError: EOL while scanning string literal
[Finished in 0.1s with exit code 1]

Solution

x = "My first Program"
print(x)

OR

x = 'My first Program'
print(x)

Output

My first Program


Solution 3

If the string spans more than one line, the python interpreter will reach the end of the line without finding the end quotes. Python has the option to ignore the end of line ( EOL ) character. You can use a multiline string by escaping the end of the line character using slash ( \ ). The code below shows how to escape the end of line character.

Program

x = "My first 
  Program"
print(x)

Solution

x = "My first\
 Program"
print(x)

Output

My first Program
[Finished in 0.1s]


Solution 4

The string is not able to span multiple lines with enclosing single or double quotation enclosures. If the string contains a lot of lines, then adding the escape character in each line will be tedious. Python allows for the enclosure of multiline strings with triple quotes. The “eol while scanning string literal” is resolved by replacing the double quotation with the triple quotation.

The code below demonstrates how to use a triple-quote multi-line string.

Program

x = "Yawin Tutor
 is my favorite
 website"
print(x)

Solution

x = """Yawin Tutor
 is my favorite
 website"""
print(x)

OR

x = '''Yawin Tutor
 is my favorite
 website'''
print(x)

Output

Yawin Tutor
 is my favorite
 website
[Finished in 0.1s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *