Python Escape Characters
In this tutorial, we will learn how to use the Escape Characters in Python.
Escape Characters are used for some special meaning in our statements. It is denoted or represented by backslash ‘\’ followed by the character.
- Single Quote (\’) and Double Quote (\”) character:
If anyone wants to print a single quote(‘) in the program directly, the compiler will raise an error. To correct this error, we use the backslash(\) followed by a single quote(‘) like ( \’).
#program to understand about single quote escape character( \’) A = “Vijay\’s book” print(A)
We have stored string data in the variable A. In between, Vijay and s specified the escape character “ \’ ” to print the single quote after Vijay.
#program for double quote escape character(\”) A = “\” Vijay\“ ” print(A)
Here we have used double quotes instead of a single quote in the program.
- Backslash(\\) Character:
If we want to print a backslash into the program, we must use “\\”. It will print one backslash to the output screen using “ \\” in the program.
#program for backslash escape characters X = ‘a division b is given as a\\b.’ print(X)
a division b is given as a\b.
As we can see in the program given, a backslash is followed by a backslash, so in the output, we get one backslash on the output screen.
- New Line(\n) Character:
The “\n” character is used to escape to the new line from the present line. If the output is so large and is not understandable, then we use the “\n” character to understand the program better.
#program for “\n” character A = “ Hey\n you” print(A)
We have given the “\n” character after “Hey”, so “you” will be printed in the new line.
- ASCII Bell Character(\a):
It is used to print an ASCII bell character or Unicode value.
#program for “\a” character print(“\a”)
When the “\a” character is given in the print statement, it will print an ASCII Bell character or UNICODE character.
- ASCII Carriage Return(\r):
“\r” escape character is used to move the cursor back to the beginning of the next line if we want to change or overwrite the content.
# program for “\r” escape character I = “This is a\r chocolate.” Print(I)
This is a chocolate
When we use the “\r” character in our program, the cursor will be moved to the beginning of the next line.
- Horizontal Tab(\t):
“\t” escape character is used to get 4 spaces into the output.
#program for the understanding of “\t” character X = “It is \training” print(X)
It is raining
“\t” will provide 4 spaces in the output before raining.
- Vertical Tab(\v):
It is similar to the “\n” character, which is used as a new line character.
#program for Vertical tab(\t) character A = “ Hello\v all ” print(A)
As we can see, when we have used the “\v” character, then automatically, the next word or character will jump to the next line.
- Character with Octal Value (\000):
We use the Octa value escape character to print the character using the octal value of that character. All the characters have some specified octal value assigned to them.
# program to print the character using their octal values print(“\110\111”)
The octal value of the character H is \110, and the octal value of the character I is \111. So, the output will be printed as Hi.
- Character with Hex Value (\xHH):
Using this escape character, we can print the Hex value of any character. All the characters are assigned with some specific hex values.
# program to print the hex value of the character print(“\x48\x49”)
The Hex value of the character H is \x48, and the hex value of I is \x49. So, the output will be printed as HI.