Escape Sequence in Python

Escape sequences in Python are used to represent certain special characters within string literals. This helps to include characters that can’t be directly included in a string, such as a newline, tab, or non-printable characters. In Python, escape sequences are prefixed with a backslash (). In this tutorial, we will discuss various escape sequences in Python, and how you can use them.

Escape Sequence in Python

An escape sequence is a special character used in the form of backslash(\) followed by a character that is required. These characters are used to represent whitespace. Whitespace gives characters like space, tab, formfeed, and vertical tab.

Common Escape Sequences in Python

Here is a list of common escape sequences in Python:

Escape SequenceDescription
\\Backslash (\)
\'Single quote (')
\"Double quote (")
\nNewline
\tTab
\rCarriage return
\bBackspace
\fForm feed
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh

Escape Sequence in Python with examples

Let us check out a few examples of escape sequences in Python.

Escape single quotes in Python

Let us check an example of escape single quotes in Python, we can see how to use \’ single quote in strings in Python.

Example:

string = 'That\'s my bag.'
print(string) 

The below screenshot shows that a single quote is used for the word That’s.

escape single quotes in Python
escape single quotes in Python

Python escape sequence n

Let us see an example of Python escape sequence n. I have used a “\n” newline character. A newline character is used to write the words in a new separate line.

Example:

string = "python\n guides"
print(string) 

You can refer to the below screenshot for the output, you can see the words in a new separate line.

Python escape sequence n
Python escape sequence n

Escape sequence backslash in Python

Let us check an example of a Python escape sequence backslash. The backslash is an escape sequence, \\ is used to print a single backslash.

Example:

string = "python\\ guides"
print(string)

You can refer to the below screenshot for the output:

Python escape sequence backslash
Python escape sequence backslash

Escape sequence for space in Python

In this example, I have used “\t” character to get space between the words.

Example:

string = "python\tguides"
print(string) 

You can refer to the below screenshot for the output, we can see the space between the word python and guides.

Python escape sequence for space
Python escape sequence for space

Escape sequence backspace in Python

In this example, I have used “\b” to remove the space between the words in Python.

Example:

string = "python \bguides"
print(string) 

You can see the output in the below screenshot.

Python escape sequence backspace
Python escape sequence backspace

Escape sequence for Hexa value in Python

Let us check an example of a Python escape sequence for Hexa value, I have used \xhh to convert hexa value into a string.

Example:

string = "\x50\x59\x54\x48\x4f\x4E \x47\x55\x49\x44\x45\x53"
print(string)

In the below screenshot, we can see the converted string.

escape sequence in python

Escape sequence for Octal value in Python

Let us check an example of a Python escape sequence for Octal value, I have used \ooo to convert the Octal value into a normal string.

Example:

string = "\120\131\124\110\117\116 \107\125\111\104\105\123"
print(string)

You can refer to the below screenshot for the output:

escape sequences in python
Octal value

Escape sequence for space in Python

Let us see an example of an escape sequence for space in Python.

In this example, I have used \t between the words to get space.

Example:

string = "python\tguides"
print(string) 

In this output, we can see the space between the words.

escape sequence for space in python

Raw Strings and Escape Sequences in Python

In Python, you can create raw strings by prefixing the string with the letter r or R. In raw strings, backslashes are treated as literal characters and not as escape characters in Python.

raw_string = r"This is a raw string\nThis will not be on a new line"
print(raw_string)

Conclusion

I hope you got an idea on escape sequence in Python also we saw, escape sequence in Python with examples.

You may like the following tutorials: