SyntaxError- EOL while scanning string literal

An EOL ( End of Line ) error indicates that the Python interpreter expected a particular character or set of characters to have occurred in a specific line of code, but that those characters were not found before the end of the line . This results in Python stopping the program execution and throwing a syntax error .


how to fix SyntaxError- EOL while scanning string literal

The SyntaxError: EOL while scanning string literal error in python occurs when while scanning a string of a program the python hit the end of the line due to the following reasons:

  1. Missing quotes
  2. Strings spanning multiple lines

Missing quotes

def printMsg(): return "This is a test printMsg()

output

File "sample.py", line 2 return "This is a test SyntaxError: EOL while scanning string literal


python error EOL while scanning string literal

The reason for this error is that forgot a closing double quote at the end of the string. String literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings).

def printMsg(): return "This is a test" printMsg()

output

This is a test

Strings spanning multiple lines

def printMsg(): str = "This is a test" print(str) printMsg()

output

File "sample.py", line 2 str = "This is SyntaxError: EOL while scanning string literal

The reason for this error is that a string enclosed in single or double quotes can't span multiple lines . Strings can't normally span multiple lines. A multiline string in Python begins and ends with either three single quotes (''') or three double quotes ("""). Any quotes, tabs, or newlines in between the "triple quotes" are considered part of the string. Python's indentation rules for blocks do not apply to lines inside a multiline string .

def printMsg(): str = """This is a test""" print(str) printMsg()

output

This is a test

Python is particularly prone to this type of error, since Python ends statements with newlines/line breaks , whereas most other programming languages have a character such as a semicolon (;) , which means that other programming languages work more easily with multi-line statements out of the box.


Python Syntax errors

Syntax errors

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Syntax errors are usually easy to fix once you figure out what they are. Unfortunately, the error messages are often not helpful. A common cause of syntax errors is the difference in syntax between Python 2 and Python 3. In particular, a syntax error may be alerted if a Python 3 file is assumed to be compatible with Python 2 (or vice versa). Explicitly specifying the expected Python version can help prevent this.