Escape sequence in Python

In this python tutorial, we will discuss Escape sequence in Python. We will also check:

  • What is the escape sequence?
  • How to escape a single quote in python
  • Python escape sequence n
  • Python escape sequence backslash
  • Python escape sequence for space
  • Python escape sequence for backspace
  • Python escape sequence for Hexa value
  • Python escape sequence for an octal value
  • Remove all escape sequence from a list in a string
  • Escape character for space python
  • Python escape sequence ignore
  • Python escape sequence remove

What is the escape sequence?

  • 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, vertical tab.
Escape sequenceMeaning
\’This represents a single quote
\nThis represents a newline
\rThis represents a carriage return
\tThis represents a tab
\bThis represents a backspace
\fThis represents a formfeed
\oooThis represents an octal value
\xhhThis represents a hex value
\\This represents a backslash
\uxxxxThis represents 16 bits hex value
\uxxxxxxxxThis represents 32 bits hex value
Escape sequence in python

How to 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) 

Below screenshot shows that single quote is used for a 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 the below screenshot for the output, you can see the words in new separate line.

Python escape sequence n
Python escape sequence n

Python escape sequence backslash

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 the below screenshot for the output:

Python escape sequence backslash
Python escape sequence backslash

Python escape sequence for space

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

Python escape sequence backspace

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

Python escape sequence for Hexa value

Let us check an example of 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 this belowscreenshot, we can see the converted string.

hex value

Python escape sequence for Octal value

Let us check an example of 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 the below screenshot for the output:

Octal value
Octal value

Remove all escape sequence from a list

In this example, I have used ‘\x50’ to remove all escape sequences by converting hex values into strings and ‘\x20’ represents the space.

Example:

x = ['welcome','\x50', 'to' '\x20','python','guides']
print(x)

In the below screenshot, we can see the output that the Hexa value ‘\x50’ is converted into ‘p’ and ‘\x20’ is converted into space.

Remove escape 1
Remove all escape sequence from all a list in a string

Escape character for space 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 character for space python
Escape character for space python

Python escape sequence ignore

To ignore the escape sequence in a string we have to make the string as a raw string by placing r before the string.

Example:

string = r'python guides'
print(string)

You can refer the below screenshot for the output. In this output, we can see that the raw statement is ignored.

Python escape sequence ignore
Python escape sequence ignore

Python escape sequence remove

In this example, I have used string.split() to remove character from left and right of the argument.

string = '\r\r\b pythonguides \r\r\n\b   '
string.strip
print(string)

You can refer the below screenshot for the output:

Python escape sequence remove
Python escape sequence remove

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Escape sequence in python. Also, We covered these below topics:

  • What is the escape sequence?
  • How to escape a single quote in python
  • Python escape sequence n
  • Python escape sequence backslash
  • Python escape sequence for space
  • Python escape sequence for backspace
  • Python escape sequence for Hexa value
  • Python escape sequence for an octal value
  • Remove all escape sequence from a list in a string
  • Escape character for space python
  • Python escape sequence ignore
  • Python escape sequence remove