In coding there are some special characters for every programming language. These special characters can be used with sepcial chracters. In python coding there are also some special characters that are illegal in strings. To use these speical characters in strings we should use an additional character named python escape character. In this lesson, we will focus on this characters, python string escape character.
An escape character is backslash (\) that is followed by the special character that you would like to insert.
For example, using double quotes is prohibited in a python string. But with escape character, we can print this character. Let’s give the below example for this case.
msg = “One of the best movie series ever is “Lord of The Rings”. Is not it?”
The above python code will give an error. Because we use two double quotes in the string and we do not mentined this with escape characters.
Now, let’use escape character this time.
The output of this message will belike below:
There are also other escape characters in python. Some of these escape characters are given below:
\r Carriage Return
\f Form Feed
\xhh Hex value
\n New Line
\ooo Octal value
\’ Single Quote
Let’s do another example with another escape character. This time, we will use tab (/t) escape character.
The output of this code will be like below:
Let’s do a last example for single quote.
The output of this python code will be like below:
We have talked about the python string escape characters and how to use special character by using an escape character with them. You can create different python examples with differetn escape characters. By using these characters more, you can understand this lesson better. More practice makes this lesson more memorable for you.