Click for Python Escape Sequence (Part 1) Tutorial
Python Escape Sequence
Continue learning about Python escape sequences in this part 2 tutorial.
Types of Escape Characters in Python
We already learned about different kinds of escape characters in the previous tutorial. We already learned about single backslash (\), double backslashes (\\), single quotes (\’), double quotes (\”), and new line (\n). Now continue with the rest of the escape characters.
Tab (\t)
A backslash with t (\t)
is used to provide space between words or characters. We provide horizontal space with \t
. It is also called tab space because it works like the tab key. We get the space when we press the tab key. Same, we can use \t to get the space between strings. For example:
Code:
text1 = 'Hello Python'
text2 = 'Hello \tPython'
print(text1)
print(text2)
Output:
Hello Python
Hello Python
We get the space between Hello
and Python
in text2
. If we use two time \t then:
Code:
text1 = 'Hello Python'
text2 = 'Hello \t\tPython'
print(text1)
print(text2)
Output:
Hello Python
Hello Python
We get two times bigger space in text2
. We can use multiple \t in a string.
Code:
text1 = 'Hello \tPython \tprogramming \tlanguage'
print(text1)
Output:
Hello Python programming language
Normally, when we use \t
for one time, we get the three spaces between the words. For example:
Code:
text1 = 'Hello Python'
text2 = 'Hello \tPython'
print(text1)
print(text2)
Output:
Hello Python
Hello Python
In text1
, when we write code, we put three spaces between words. And in text2
, we simply use \t
, and we get the same spaces in the output. So a single \t gives us three spaces; however, the interpreter doesn’t count it as three spaces. When we use \t, it is counting as a single tab space.
NOTE: It is not necessary that when you use \t, you get three spaces. The number of spaces could be different according to your tab space settings.
Backspace (\b)
A backslash with b (\b)
is used to remove space between words or characters. It is just opposite of \t
. While \t
is used to provide space and \b
is used to remove space. For examle:
Code:
text1 = 'Hello Python'
text2 = 'Hello \bPython'
print(text1)
print(text2)
Output:
Hello Python
HelloPython
In text2
, we use \b
, so we get no space between Hello
and Python
. So we can use \b
to remove one space between words. If we have more than one space in our strings, then:
Code:
text1 = 'Hello \bPython'
print(text1)
Output:
Hello Python
In text1
, we have four spaces between Hello and Python. We use one time \b
, so it removes one space, so we get three spaces between words in the output. If we use two time \b
, it will remove the two spaces between words.
Code:
text1 = 'Hello \b\bPython'
print(text1)
Output:
Hello Python
So two times, using of \b
, removes two spaces. Now what if our string doesn’t have two spaces, but still we use two times\b
?
Code:
text1 = 'Hello \bPython'
print(text1)
text2 = 'Hello \b\bPython'
print(text2)
Output:
HelloPython
HellPython
In the second output, we only get Hell
instead of Hello
because we used two times \b
. So first \b
remove the one space and second \b
remove the last character o from Hello
. Because there is no space left to remove. So it will remove the last character. Now if we have used three time \b, then:
Code:
text2 = 'Hello \b\b\bPython'
print(text2)
Output:
HelPython
So if there is no space left to remove, \b
removes the characters that came before \b
.
Now what if we use \b in the beginning of the line?
Code:
text = '\bHello'
print(text)
Output:
Hello
We get the Hello in the output because \b
removes space and if space is not found, it removes the characters. But there is no space or any character before the \b
. So we get the same string in the output.
Now what if we use \b
and \t
in the same string?
Code:
text = 'Hello \t\bPython'
print(text)
Output:
Hello Python
In the output, we only get one space. Interpreter reads from left to right, so first when it reads, first came \t
, so we get a tab space, then came \b
, it will remove the space. We learned when we use one time \b
, it will remove one space. Now here, when we use \t
for tab space, this tab space is counted as one space. It doesn’t matter how many spaces this tab space consists of. We learned in the upper section that a \t
consist of three spaces, but the interpreter counts it as a single space. That’s why we get only one space in the output.
Now what if we use \b
before \t
?
Code:
text = 'Hello \b\tPython'
text2 = 'Hello Python'
print(text)
print(text2)
Output:
Hello Python
Hello Python
In text2
, we use three spaces between Hello
and Python
. In text
, first \b
will remove the space, then \t
provide a tab space, which is set as three spaces. So we get the same result in the output.
Carriage return (\r)
A backslash with r (\r)
is used for carriage return. Carriage return means returning the cursor to the beginning of the line. Wherever we use \r
in strings, the part after \r
comes forward in the output. For example:
Code:
text = 'Hello \rPython'
print(text)
Output:
Python
We use \r
before Python, so we get Python in the output and Hello
before \r removed from the output. If we use multiple time \r
?
Code:
text = 'Hello \rPython \rprogramming \rlangauge'
print(text)
Output:
langauge
The interpreter reads from left to right. So the string part related to the last \r
, comes in the output. So this is called carriage return.
Octal value (\ooo)
\ooo represents the unique octal value of the alphabet. If we use an octal value in strings. In the output, we can get the alphabetic character related to the octal value. For example:
Code:
text = 'Life is \141 life'
print(text)
Output:
Life is a life
Here \141 is the octal value of character a. Another example:
Code:
text = '\141 \142 \143'
print(text)
Output:
a b c
So we used octal value in the strings and we get alphabetic characters in the output.
Hexadecimal Values (\xhh)
Just like we did in octal value, if we use a hexadecimal or hexa value in our strings. We get the alphabetic value in the output. For example:
Code:
text = '\x44'
print(text)
Output:
D
We use a hexa value in strings and the interpreter converts this hexa value into an alphabetic value. Another example:
Code:
text = '\x34 \x44 \x52 \x23'
print(text)
Output:
4 D R #
So these are all the most commonly used escape characters in Python.
Rules for Escape Sequence in Python
There are some basic rules that we should consider when using escape sequences in Python.
Escape sequence works only in strings
Escape sequence works only with strings in Python. If we use escape characters with other data types, it won’t work. For example:
Code:
text1 = ['Python \nProgramming']
text2 = 'Python \nProgramming'
print(text1)
print(text2)
Output:
['Python \nProgramming']
Python
Programming
We use \n
in text1
and text2
, text1
is a list and text2
is a string. \n
didn’t do anything on the list. but in text2
, it creates a new line. Another example:
Code:
text1 = ('apple', '\nPython')
text2 = 'apple \nPython'
print(text1)
print(text2)
Output:
('apple', '\nPython')
apple
Python
We get the same output. text1
is a tuple, and \n
didn’t work in a tuple. So the escape sequence only works in the strings.
No space between backslash and escape character
This is a very important rule; there should be never a space between backslash and escape character. If we give a space between them, that escape character won’t work. For example:
Code:
text1 = 'apple \nPython'
Now, this is correct. We didn’t use any space between the backslash and n. Now if we give a space, then:
Code:
text1 = 'apple \ nPython'
print(text1)
Output:
SyntaxWarning: invalid escape sequence '\ '
We get an error. Because as soon as we use the backslash, the interpreter assumes we are going to write an escape character just after the backslash. In this case, we use space, so the interpreter assumes this is the escaepe character we want to use: '\ '
. But there is no escape sequence in Python with the backslash and spaces. So that is why we never use any kind of space between escape characters.
Also, there is an important thing: if we use space after writing an escape sequence, it will show in the output. For example:
Example 1:
Code:
text1 = 'Apple \nPython \nJava \nProgramming'
print(text1)
Output:
Apple
Python
Java
Programming
Example 2:
Code:
text1 = 'Apple \n Python \n Java \n Programming'
print(text1)
Output:
Apple
Python
Java
Programming
Example 3:
Code:
text1 = 'Apple \n Python \n Java \n Programming'
print(text1)
Output:
Apple
Python
Java
Programming
So we cannot use space between the backslash and an escape character. We can use space after using the escape character, but that space will be shown in the output.
Escape sequence won’t work in raw strings
We already learned that escape sequences only worked in strings. Also, the escape sequence won’t work in the raw strings. Raw strings is another way to write the strings. In raw string, we put r before the text. For example:
Code:
text1 = 'Normal string'
text2 = r'Raw string'
print(type(text1), type(text2))
Output:
<class 'str'> <class 'str'>
So, text1 is normal string and text2 is raw string. Both are the same 'str'
class object. Here the only difference is the way we write raw string. In text2
, we use r
before writing our string. Now what if we use \n
in both strings and see the output?
Code:
text1 = 'Normal \nString'
text2 = r'Raw \nstring'
print(text1)
print(text2)
Output:
Normal
String
Raw \nstring
We can see the difference; the escape character didn’t work in raw strings and we get the same output without any new line.
Conclusion
This is the end of the Python escape sequence tutorial. Escape sequence is like utility in Python. If you know which function is done by which escape character. You can use that function in strings to modify the result. Always remember, escape sequence only works in strings.
End of Python Escape Sequence (Part 2)
One thought on “Python Escape Sequence (Part 2): What Are The Escape Characters In Python?”