×

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python.

Escape Character:

Escape Characters are used for some special meaning in our statements. It is denoted or represented by backslash ‘\’ followed by the character.

  • Single Quote (\’) and Double Quote (\”) character:

If anyone wants to print a single quote(‘) in the program directly, the compiler will raise an error. To correct this error, we use the backslash(\) followed by a single quote(‘) like ( \’).

Example 1:

#program to understand about single quote escape character( \’)
A = “Vijay\’s book”
print(A)

Output:

Vijay’s book

Explanation:

We have stored string data in the variable A. In between, Vijay and s specified the escape character “ \’ ” to print the single quote after Vijay.

Example 2:

#program for double quote escape character(\”)
A = “\” Vijay\“ ”
print(A)

Output:

“Vijay”

Here we have used double quotes instead of a single quote in the program.

  • Backslash(\\) Character:

If we want to print a backslash into the program, we must use “\\”. It will print one backslash to the output screen using “ \\” in the program.

Example:

#program for backslash escape characters
X = ‘a division b is given as a\\b.’
print(X)

Output:

a division b is given as a\b.

As we can see in the program given, a backslash is followed by a backslash, so in the output, we get one backslash on the output screen.

  • New Line(\n) Character:

The “\n” character is used to escape to the new line from the present line. If the output is so large and is not understandable, then we use the “\n” character to understand the program better.

Example:

#program for “\n” character
A = “ Hey\n you”
print(A)

Output:

Hey
You

We have given the “\n” character after “Hey”, so “you” will be printed in the new line.

  • ASCII Bell Character(\a):

It is used to print an ASCII bell character or Unicode value.

Example:

#program for “\a” character
print(“\a”)

Output:

?

Explanation:

When the “\a” character is given in the print statement, it will print an ASCII Bell character or UNICODE character.

  • ASCII Carriage Return(\r):

“\r” escape character is used to move the cursor back to the beginning of the next line if we want to change or overwrite the content.

Example:

# program for “\r” escape character
I = “This is a\r chocolate.”
Print(I)

Output:

 This is a
  chocolate

Explanation:

When we use the “\r” character in our program, the cursor will be moved to the beginning of the next line.

  • Horizontal Tab(\t):

“\t” escape character is used to get 4 spaces into the output.

Example:

#program for the understanding of “\t” character
X = “It is \training”
print(X)

Output:

It is     raining

Explanation:

“\t” will provide 4 spaces in the output before raining.

  • Vertical Tab(\v):

It is similar to the “\n” character, which is used as a new line character.

Example:

#program for Vertical tab(\t) character
A = “ Hello\v all ”
print(A)

Output:

Hello
all

Explanation:

As we can see, when we have used the “\v” character, then automatically, the next word or character will jump to the next line.

  • Character with Octal Value (\000):

We use the Octa value escape character to print the character using the octal value of that character. All the characters have some specified octal value assigned to them.

Example:

# program to print the character using their octal values
print(“\110\111”)

Output:

HI

Explanation:

The octal value of the character H is \110, and the octal value of the character I is \111. So, the output will be printed as Hi.

  • Character with Hex Value (\xHH):

Using this escape character, we can print the Hex value of any character. All the characters are assigned with some specific hex values.

Example:

# program to print the hex value of the character
print(“\x48\x49”)

Output:

HI

Explanation:

The Hex value of the character H is \x48, and the hex value of I is \x49. So, the output will be printed as HI.


Related Topics

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.

Important Difference between Python 2.x and Python 3.x with Example

The comparison between Python 2 and Python 3 is given in the article that follows. Python is a computer language that can perform more tasks than other languages and is...

5 minutes read.

How to comment out a block of code in Python

When you are writing code in Python, you may want to document it. You need to mention why a piece of code functions, for instance. Mathematics, algorithms, and intricate business...

4 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

How to Install Python In Windows

How To Install Python In Windows? Python is a language that every aspirant developer looks forward to. One thing we must keep in our mind is how to begin our hands-on...

3 minutes read.

End Parameter in python

print(): The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value. Syntax: print(“hi”) Output: hi will be displayed on the output...

3 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Compound Interest GUI Calculator using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

How to learn python Online

Need to Learn a Programming Language 1. To get a high Paying job We know that Software Engineering is one of the top-paying professions in the world. The top criteria to be...

3 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Linear Regression using Sklearn with Example

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python Program to Print Sum of all Elements in an Array

Python program to print sum of all elements in an array A set of objects stored in contiguous memory locations is referred to as an array. The concept is to keep...

2 minutes read.