Overview
An escape sequence character in Python is used to insert unwanted or special sequences of characters that simply Python doesn’t allow us in the string.
What’s Next!
In this comprehensive Python escape sequence character guide, we will explore the scene behind the insertion of a special sequence of characters in Python. Furthermore, we will learn how many types of escape characters in Python with definitions and practical examples.
Table of Contents
What is an Escape Sequence Character in Python?
An escape character is defined as a backslash (\) followed by a special character code. It is used when Python does not allow us to add a special sequence of characters in our code. For example, \t – tab size, \n – new line, and \b – backspace, etc. Where (\) refers to backslash and (t) is a special character code.
Moving continue, let’s explore the list of escape sequence characters in Python:
Escape sequence | Meaning | Result |
\’ | Single quote (‘) | Single quote in seq. of char. |
\\’ | Double quote (‘’) | Double quote in seq. of char. |
\\ | Backslash (\) | Represents a backslash |
\n | newline | Print seq. of char. In newline |
\r | Carriage Return | |
\t | Tab size | It inserts a Tab size |
\b | Backspace | It reduces a Backspace |
\f | Formfeed | |
\v | Vertical Tab | |
\0 | Null Character | |
\000 | A backslash followed by three integers 000 | Print the seq. of char. from integers value |
\xhh | A backslash followed by an ‘x’ and a hex number | Print the seq. of char. from integers value |
Let’s move ahead, and try each special escape character with the example:
Example 1: Use the escape sequence character single quote (\’).
Simple example:
text = 'I am a good boy.'
print(text)
single-quote (\’) escape sequence character example:
text = 'I\'m a good boy.'
print(text)
Example 2: Use the escape sequence character double quote (\\’).
Simple example:
text = 'I am a good boy.'
print(text)
double-quote (\’) escape sequence character example:
text = 'I \'am\' a good boy.'
print(text)
Example 3: Use the escape sequence character Backslash (\).
Simple example:
text = 'I am a good boy.'
print(text)
Backslash (\) escape sequence character example:
text = 'I am a good boy \.'
print(text)
Example 4: Use the escape sequence character newline (\n).
Simple example:
text = 'I am a good boy.'
print(text)
newline (\n) escape sequence character example:
text = 'I am a \ngood boy.'
print(text)
Example 5: Use the escape sequence character Carriage Return (\r).
Simple example:
text = 'I am a good boy.'
print(text)
Carriage Return (\r) escape sequence character example:
text = 'I am a \rgood boy.'
print(text)
Example 6: Use the escape sequence character Tab size (\t).
Simple example:
text = 'I am a good boy.'
print(text)
Tab size (\t) escape sequence character example:
text = 'I am a \tgood boy.'
print(text)
Example 7: Use the escape sequence character Backspace (\b).
Simple example:
text = 'I am a good boy.'
print(text)
Backspace (\b) escape sequence character example:
text = 'I am a \bgood boy.'
print(text)
Example 8: Use the escape sequence character backslash followed by the integers (\000).
Simple example:
text = 'I am a good boy.'
print(text)
\000 escape sequence character example:
text = '\151 \40 \141 \155 \40 \141 \40 \147 \157 \157 \144 \40 \142 \157 \171'
print(text)
Example 9: Use the escape sequence character backslash followed by the hex value (\xhh).
Simple example:
text = 'I am a good boy.'
print(text)
Espace sequence characters from the list
Now moving ahead, let’s think of a question, what if we want to remove the escape character from the string? So, how to do that?
Example: Remove all escape characters from the list.
text = ['\x49', 'am', 'a', '\x67 \x6f \x6f \x64', 'boy']
print(text)
The original sentence is ‘I am a good boy.’ From the sentence, the word ‘I’ is converted to hex value \x49, and the word ‘good’ is \x67 \x6f \x6f \x64. All the numerical values represent the hex value of the respective word. If we add backslash “\” before the number, then we easily escape the all numbers and get the original word.
Escape sequence characters ignore
By adding the letter ‘r’ before the word, we can easily ignore the escape sequence. In other words, to ignore the escape sequence in a string, we need to convert the string to the raw string by adding ‘r’ before the string.
Example: Ignore the escape sequence by adding ‘r’.
print(r"I am a \tgood boy.")
Conclusion
So, very well we have gone through Escape Sequence Character in Python. We started with exploring the scene behind the escape sequence and further moved ahead toward going deep into various escape sequences in Python such as \n, \t, \b, etc. Finally, we performed a few operations such as converting hex values back to words and ignoring the escape sequence character in the string.