×

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

Python Euclidean Distance

Euclidean distance is the distance between two points with whatever of dimensions. We are using NumPy library to find and calculate the Euclidean distance. The NumPy library is used for...

1 minute read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 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.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

6 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

How to Install Scikit-Learn

Sklearn or Scikit-learn is a python library used for machine learning. It contains many features like classification, regression, clustering, and Dimensionality reduction algorithms. Sklearn is used to build machine learning...

3 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Python Program to Find the Greatest Among Three Numbers

Python Program to find the greatest among three numbers We have many approaches to get the largest of three numbers, and here we will discuss a few of them. This python...

2 minutes read.

After Python, What Should I Learn

Python is a programming language used to create websites, software, and other projects. It is easy to code and easy to learn. After learning Python, there are many different directions...

4 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.