Escape Sequences in Python
If you have been programming for some time now, you must've worked with strings. You would have printed about a lakh of strings using a print statement, and solved thousands of errors. But here is a question, "did you ever try to insert a double quote inside a string enclosed in double quotes?" Or
The Answer is, "double-quote inside the string must be escaped by the eye of Python." To achieve this, we have special characters in Python called the Escape Sequences. This article discusses the concept of escape sequences, their uses, and their importance with examples.
Definition: As the name suggests, an escape sequence is a sequence of characters with special meaning when used inside a string or a character.
Syntax: The characters need to be preceded by a backslash character
Example: \n, \t etc.
The characters that we can't insert into a string are called Illegal characters, and these characters modify the string. The function of escape sequences is to insert such characters into the string without modifying the string.
Let us first see a simple example:
1. Inserting single quotes into a string:
#without using \'
string = 'Hi! 'man' ^ SyntaxError: invalid syntax
Understanding: Python searches for an ending-single quote in the string 'Hi!'man'. Hence, it raises an error.
#Using '' in the string
Here, 'Hi!' and 'man' are considered two separate strings, so no error is raised. The number of quotes is even. But if we try to insert something between 'Hi!' and 'man', Python gets confused again and raises an error.
string = 'Hi!'!'man' ^ SyntaxError: invalid syntax
Using the escape sequence:
By using \: Hi! 'man
You can observe that our problem is now solved. Also, another solution in this situation is that we can use double quotes to enclose the string and use 'in the string:
By using \: Hi!'man
There are more escape sequences in Python that helps the programmer to print different types of invalid statements:
Now, let us see the examples for the sequences to understand their uses:
string = "Hi!"man" ^ SyntaxError: invalid syntax
By using \: Hi!"man
Understanding: You can see that without using the backslash character, we got an error, and when we placed a / before the ", we got it printed.
string = "Hi!"man"
By using : Hi! man
In the string, "By using \n", unknowingly, we used \n. We need it to be printed, but we don't want its functionality. What to do? Use an escape sequence:
By using \n: Hi man
Without using \: Hi! man
By using \t: Hi! man
Understanding: We wrote the normal statement in the first code giving normal spaces. In the second code, we used the \t to give a tabular space between Hi! and man.
Without using \r: Hi! man
manusing \r: Hi!
When we use \r, all the content after \r will be brought to the front by replacing all the existing characters.
The string will be "By using \\r: Hi!\rman"
The content after \r is "man". This word will be brought to the front of the string by replacing the existing characters-"By_" making the string man using \r: Hi!
Here, "man" succeeds \r. it will be brought to the front of the string, replacing Hi!; All the three characters in Hi! will be replaced by man making the output "man".
Without using \b: Hi! man
By using \b: Himan
\b is the backspace character. It deletes the preceding character like the backspace key in our keyboards and calculators. We gave \b between Hi! and man. The preceding character is "!"; hence, it is deleted.
ooo represents octal numbers. We can get the characters equivalent to octal representations by using this character.
\543 represents ?
\432 represents ?
\789 represents 89
hh represents octal numbers. We can get the characters equivalent to hexadecimal representations by using this character.
\x54 represents T
\x34 represents 4
The sequences discussed in the article are the most commonly used escape characters. These are not the only escape sequences in Python. More sequences will be useful in different scenarios while writing different logic.