SyntaxError: EOL while scanning string literal

SyntaxError: EOL while scanning string literal error indicates that the interpreter expects a specific character or set of characters in the python code, but such characters do not exist before the end of the line. EOL is an abbreviation for “End of Line”. The string must be enclosed in quotation marks, either single or double. The error is caused by a missing quotation in the string, which is common in Python.

The python interpreter reaches the last character of the line while scanning the string without encountering the closing quotation marks. Therefore, the SyntaxError: EOL while scanning literal string error is thrown. Whenever this syntax error occurs, Python stops the execution of a program. To solve this issue, add the single and double quotation marks to enclose the string.

An EOL error is a syntax error that happens when the Python interpreter expects a specific character or string to appear in a given line of code but the character or string does not appear before the end of the line. It’s generally a typo error or syntax mistakes.



1. Causes

The “SyntaxError: EOL when scanning the literal string” error occurs in Python while scanning the string in the code line, and the python interpreter reaches the end of the line for the following causes.

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


2. Missing Quotes to close the string

The missing double quotation in the string is the cause of the syntax error. The double quotation is either missing at the beginning or at the end of the string. Identify the beginning and the end of the text. Use a double quotation to enclose the text. The syntax error “SyntaxError: EOL while scanning string literal” will be resolved by replacing the missing quote.

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


3. Mixing the Quotes

The syntax error is caused by the string’s use of a single quote followed by a double quotation. A different quotations are used at the beginning and ending of the string. Include the text with double quote marks at the beginning and end. The syntax error will be resolved by replacing the incorrect quote with the right quotation.

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


4. Multiline without slash

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]


5. Multiline with triple quotes

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]



Leave a Reply