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 sequence | Meaning |
\’ | This represents a single quote |
\n | This represents a newline |
\r | This represents a carriage return |
\t | This represents a tab |
\b | This represents a backspace |
\f | This represents a formfeed |
\ooo | This represents an octal value |
\xhh | This represents a hex value |
\\ | This represents a backslash |
\uxxxx | This represents 16 bits hex value |
\uxxxxxxxx | This represents 32 bits hex value |
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.
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 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 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 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 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.
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:
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.
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.
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 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:
You may like the following Python tutorials:
- Python list comprehension lambda
- Python Threading and Multithreading
- How to convert Python degrees to radians
- Python Comparison Operators
- Python namespace tutorial
- Python Tkinter Frame
- How to make a matrix in Python
- Linked Lists in Python
- Python ask for user input
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
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.