Python Escape Characters

The escape character is used to invoke alternative interpretations for the specified character set. Escape characters behave differently than their normal meaning and generally consist of two or more characters. Python provides different characters that are generally available in other popular programming languages in strings. Escape characters are only used inside a string. Escape characters start with the backslash \ character.

Python Escape Character List

Python provides the following escape characters in order to use inside strings to express special cases.

ESCAPE CHARACTERMEANING
\”Double quote
\’Single Quote
\\Backslash
\nNew Line
\rCarriage Return
\tTab
\bBackspace
\fForm Feed
\oooOctal Value
\xhhHex value

Single and Double Quote Escape Character

One of the most popular use cases for the escape character in Python is using single and double quote inside a string. Normally single and double quote is used to create a string in Python and the start and end of the string are specified with a double or single quote. What will happen if you need to use the single or double quote inside the string which will end the string and throw an exception. The single or double quote escape characters can be used to put the single and double quote inside a string. The single quote escape character is \' and double quote escape character is \" .

txt ="This is a \"double quote\" example."
print(txt)


txt ="This is a \'single quote\' example."
print(txt)


txt = """This is \'single quote\' example
 which is multiline string"""
print(txt)


txt = """This is \"double quote\" example
 which is multiline string"""
print(txt)

The output will be like below.

This is a "double quote" example.

This is a 'single quote' example.

This is 'single quote' example
which is multiline string

This is "double quote" example
which is multiline string

Backslash Escape Character

As the backslash \ is used for special characters single backslash will be interpreted as an escape character start. In order to create a backslash character, the backslash escape character \\ should be used as below.

txt ="\\ is a backslash character"
print(txt)

New Line Escape Character

New line is used to put the end of line and jumpt to the new line. The \n is used to put new line event the string is a single line.

txt ="This \n is \n a sample text"
print(txt)

Tab Escape Character

Tab is a character used to put some space for the text. Tab is generally used to show the first sentence of the paragraph etc. The tab can be expressed by using the tab escape character \t.

txt ="This \t is \t a sample text"
print(txt)

The output will be like below.

This       is        a sample text

Backspace Escape Character

Backspace is a keyboard key which is used to put the cursor one step back by deleting the left character. This is simple a delete operation to the left ot start of the line. the backspace escape character is \b .

txt ="This\b is\b a sample\b text"
print(txt)

The output is like below.

Thi i a sampl text

Python Escape Characters Infographic

Python Escape Characters Infographic

Leave a Comment