×

Python EOL (End Of Line)

Introduction

An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a string literal. This is due to the missing quotes and strings spanning multiple lines.

The string literals need to be enclosed with a single quote or double quote. they can also be enclosed in matching groups of three single quotes.

Example:

s = "This is a string literal...

# Printing the string

print(s)

Output:

File "EOL.py", line 2

    s = "This is a string literal...

                                   ^

SyntaxError: EOL while scanning string literal

The last character of the string is pointed by the small arrow and indicates that the error has occurred during defining that element of the statement.

String spanning Multiple Lines

Add definition or somekind of explanation.

Example -

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

Explanation –

In the above output, the error has occurred because the multiple lines can not be spanned by the string enclosed in single or double quotes. In Python, a multiple-line string starts and ends with three single quotes or three double-quotes.

Now, we will see how it actually works with the three single quotes and three double quotes.

Example:

def printMsg():

    str = """This is

      a string"""

    print(str)

printMsg()

Output:

This is

      a string

Fixing “Syntax Error: EOL while scanning string literal”

We can fix these syntax errors with the help of some solutions. These solutions are based on some common error-raising situations while the execution of a code.

There are four common situations where this error can be faced:

Placing the ending quotation mark

As we know that the syntax error is raised by the Python interpreter when the quotation mark is missing at the end of the string literal.

This error can be removed by placing the ending quotation marks.

Problem

#ending quotation mark is missing

string = "This is a string literal...

# Printing the string

print(string)

Solution:

# Placing the ending quotation mark

string = "This is a string literal..."

# Printing the string

print(string)

Output:

This is a string literal...

Matching the Quotation Marks

String literal enclosing can be done with the use of ' ' and " " in Python. If the quotation is incorrect at the ending of the string it will raise an error.

Problem:

# ending quotation mark is incorrect

string = "This is a string literal...'




# Printing the string

print(string)

Solution:

# Matching the quotation marks

string = "This is a string literal..."


# Printing the string

print(string)

Output:

This is a string literal...

Using the escape sequences \n

The effect of the new line is provided by the escape sequence '\n' to the string literal.

Problem:

# String dividing to multiple lines

string = "This is a string literal...

          Going to the next line"


# Printing the string

print(string)

Solution:

# with escape sequences \n 

string = "This is a string literal... \n this is the next line"




# Printing the string

print(string)

Output:

This is a string literal...

this is the next line

Using backslash before ending quotation mark

The backslash '\' can raise a syntax error and it is accountable for escaping the string. The solution to this problem can be removed by replacing the '\' backslash with an escape sequence for a (\\) backslash.

Problem:

# setting the directory path  

string = "\home\User\Desktop\"


# Printing the string

print(string)

Solution:

# setting the directory path 

string = "\\home\\User\\Desktop\\"

# Printing the string

print(string)

Conclusion

In the above article, we have learned how to remove the error EOL while scanning string literal and we have seen the solution to some common problems.


Related Topics

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

Difference between Perl and Python

Control and presently utilized for a great many undertakings, including framework organization, web improvement, network programming, and GUI improvement, and the sky is the limit from there. About Perl? Perl is a...

4 minutes read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

Add Element to Tuple in Python

Python: Popular high-level, all-purpose programming language Python. The new version of the Python programming language, Python 3, is used for all software applications, including web development. Python is the best programming...

4 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Python Program to Print all the Prime Number in an Interval

Python Program to Print all the Prime Number in an Interval What is Prime numbers? A prime number is referred to those numbers that can be divisible by them only. In simple words,...

4 minutes read.

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.