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.

\”Double quote
\’Single Quote
\nNew Line
\rCarriage Return
\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 quotes 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."

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

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

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

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"

New Line Escape Character

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

txt ="This \n is \n a sample text"

Tab Escape Character

The tab is a character used to put some space for the text. The 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"

The output will be like below.

This       is        a sample text

Backspace Escape Character

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

txt ="This\b is\b a sample\b text"

The output is like below.

Thi i a sampl text

Specify Characters As Hex Value

The escape character \x can be used to specify characters with their hex values. Every character can be presented as a hex value like below. In the following example, we print “Hello World” characters as hex values.

Hello World!

Specify Characters As Octal Value

The escape character can be used to provide character as an octal value. The \ is used specify the characters octal value. In the following example, we print the “Hello World” as octal value with the help of the escape character. This can be also called printing them with ASCII values.

Hello World!

Python Escape Characters Infographic

Python Escape Characters Infographic

Leave a Comment