Fix the SyntaxError: EOL While Scanning String Literal Error in Python

Fix the SyntaxError: EOL While Scanning String Literal Error in Python

  1. Add Missing Quotes to Fix the SyntaxError: EOL while scanning string literal Error in Python
  2. Use Triple Quotes to Fix the SyntaxError: EOL while scanning string literal Error in Python
  3. Escape Backslash \ to Fix the SyntaxError: EOL while scanning string literal Error in Python

Syntax errors are one of the common errors in Python programming. The SyntaxError is raised when you have used incorrect syntax in your code.

An EOL (End of Line) error means a specific character is used in a code but is not present at the end of a line. For example, if you use single or double quotes, it must be closed before the end of a string; otherwise, you will get a SyntaxError.

The error EOL while scanning string literal occurs when you forget to close the quotes or the strings span multiple lines.

This tutorial will teach you to fix the SyntaxError: EOL while scanning string literal error in Python.

Add Missing Quotes to Fix the SyntaxError: EOL while scanning string literal Error in Python

The string is defined by using single or double quotes in Python. If you forget to close quotes at the end of a string, it will throw a SyntaxError.

Here is an example of SyntaxError: EOL while scanning string literal.

s="This is a simple text
print(s)

Output:

  File "Main.py", line 1
    s="This is a simple text
                           ^
SyntaxError: EOL while scanning string literal

The error is raised while scanning a string literal of the program that ends with missing double quotes.

To fix this issue, you must close unclosed quotes in the string.

s="This is a simple text"
print(s)

Output:

This is a simple text

Use Triple Quotes to Fix the SyntaxError: EOL while scanning string literal Error in Python

If you still get the error after closing quotes, check if the strings span multiple lines.

The following is an example of multiline strings.

s="This is a simple text
This is a second line
This is a third line"
print(s)

Output:

  File "Main.py", line 1
    s="This is a simple text
                           ^
SyntaxError: EOL while scanning string literal

Strings cannot span multiple lines with one single or double quote. You have to use triple quotes to define a string of multiple lines.

You can either use three single quotes or three double quotes.

s="""This is a simple text
This is a second line
This is a third line"""
print(s)

Output:

This is a simple text
This is a second line
This is a third line

Escape Backslash \ to Fix the SyntaxError: EOL while scanning string literal Error in Python

Sometimes, the error EOL while scanning string literal also occurs when the string ends with a backslash character \.

s="This is a simple text\"
print(s)

Output:

  File "Main.py", line 1
    s="This is a simple text\"
                             ^
SyntaxError: EOL while scanning string literal

In that case, you must escape the backslash (\) with another backslash (\).

s="This is a simple text\\"
print(s)

Output:

This is a simple text\

Now you know how to solve SyntaxError: EOL while scanning string literal in Python.

The main reasons for this error are the missing quotes at the end of a string and the use of multiline strings. We hope you find this tutorial helpful.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10