In python, escape characters or escape sequences are used to insert special characters in a python string. These special characters are also called illegal characters and they can not be placed directly in the string. However, to include such characters, we can use a backslash(\) before the character.
To understand the concept of escape characters in python, we need to know more about these illegal or special characters that can not be printed without an escape sequence in Python.
What are these special or illegal characters?
The print statement below runs fine:
print('This is Avid python.')
This was a simple print statement which gave the following output:
This is Avid python.
Now, let us run this line of code:
print('This is Avid python's post.')
It returns an error:
SyntaxError: invalid syntax
It must be clear from the above examples that print statement supports the working of both single and double quotes. However, using a single quote inside two single quotes is invalid. The same is true for double quotes and backslash(‘\’). Placing them inside the print statement return an error.
Such characters are called special characters. When these characters are placed inside a print statement, they change the meaning of the code and result in an error. However, if escaped using a backslash, they will be printed normally.
A few escape Sequences
Let us look at a few escape sequences one by one. To print a special character, we always use the backslash operator \. The character that is typed after backslash character is considered a simple character of the string. When the backslash character is not present, the special character may behave differently.
Single quote (\’)
\’ prints a single quote. Below is an example:
print('How\'s this article?')
Output:
How's this article.
Let us print this statement without using \’ . But we get an error:
print('How's this article?')
Output:
SyntaxError: invalid syntax
Double quotes (\”)
This prints a double quote. Below is an example:
print("This article is by \"Avid python\".")
Output:
This article is by "Avid python".
Try printing a double quote without a backslash and you will get an error.
print("This article is by "Avid Python".)
Output:
SyntaxError: invalid syntax
However, note that we can use double quotes inside a single quote and vice-versa. This will be more clear from the below examples:
print("How's this article?")
print('This article is by "Avid Python".')
Output:
How's this article?
This article is by "Avid Python".
In the first line of code, a single quote goes inside double quotes and in the second line of code, double quotes are used inside single quotes. This returns the output as expected. But using the same quotes together cannot be done without using an escape sequence.
Backslash (\\)
This prints a backslash. Let us print an array of mountains to understand this one:
print('/\\/\\/\\/\\/\\/\\')
Output:
/\/\/\/\/\/\
Two backslashes print a single backslash. If we try printing this otherwise, we get an error:
print("/\/\/\/\/\/\")
Output:
SyntaxError: EOL while scanning string literal
Linefeed (\n)
Let us try printing a poem using a single print statement in python. If we use the code written below, we get an error.
print("Twinkle, twinkle, little star,
how I wonder what you are!!")
Output:
SyntaxError: EOL while scanning string literal
In python, we cannot break a print statement over multiple lines like this. However, this problem can be solved using \n. Linefeed or \n breaks the current line and takes the control over to the next line. Let us write this poem using \n.
print("Twinkle, twinkle, little star,\nhow I wonder what you are!!")
Output:
Twinkle, twinkle, little star,
how I wonder what you are!!
This time, we get the desired output. In addition to this, we can also use triple quotes to break a print statement in multiple lines as follows:
print("""Twinkle, twinkle, little star,
how I wonder what you are!!""")
Output:
Twinkle, twinkle, little star,
how I wonder what you are!!
Horizontal tab (\t)
This is used to print a horizontal space. It is an escape sequence for tab character.
print("This is\tAvid Python")
Output:
This is Avid Python.
As can be seen, there is no space between the words ‘is’ and ‘Avid’. Instead, \t is used. However, we still get the output with space between these words. This is how \t works.
List of escape characters
Here is a list of recognized escape sequences:
Escape Sequence | Meaning |
\’ | Single quote |
\” | Double quote |
\\ | Backslash |
\n | Linefeed |
\t | Horizontal tab |
\v | Vertical tab |
\r | Carriage Return |
\f | Formfeed |
\b | Backspace |
\a | Bell |
\newline | Backslash and newline ignored |
\ooo | Character with octal value ooo |
\xhh | Character with hex value hh |
Conclusion
In this article, we have discussed various escape characters in python. I hope you enjoyed reading this article.
To learn more about python programming, you can read this article on list comprehension in python. You might also like this article on dictionary comprehension in python.