Python Escape Sequence With Example

Python Escape Sequence

Basically, when we are writing strings, we might come across some situations, where we need to insert something into the string, which we cannot do directly, or if we are required to format our string in some way, we would want to make use of the escape sequences. The thing is that when we would have a look at the examples, we can then understand what could be the need for using the escape sequences. Basically, Let’s consider an example now, where we are trying to get a new line into the string. If we have our string surrounded by double or single quotes, and we try to hit enter in order to get the new line, we would simply run into errors.

Have a look at this –

Python Escape Sequence

As you can see, within the string, we are trying to get a new line, between GyaniPandit and Python, but we are not able to get it like that. So, in such a situation, we can make use of the escape sequence as well. Using an escape sequence is like you are going to use something in your string, that you may not be able to use in some other (normal) situations. So, we are going to make use of the backslash in this case.

So, correcting the above program, we need to add a new line after GyaniPandit, which we are going to achieve with the help of an escape sequence. So, the escape sequence that we are going to use here, is the ‘\n’, which would simply mean a new line. Have a look at the below program.

As you can see, in the above program, we have used the escape sequence \n, which simply means that we are adding the new line. Also, we have some other escape sequences for doing different things here.  Let’s have a look at some of the escape sequences. Here are some escape sequences in the given table, and also we are going to take some examples related to them so that we can understand those escape sequences.

Escape SequenceMeaning
\nAdds a new line
\tAdds a tab
\\Adds a backslash
\’Adds a single quote
\”Adds a double quote
\bAdds a backspace
\xhhA character with the hex value hh

Well, in the above table, we have some escape sequences, that can be used from time to time. Let’s have a look at some examples, through which, we can simply understand those escape sequences.

First of all, we have seen the \n thing here, which adds a new line to our string. You can try in furthermore examples, but since we have used it previously, Let’s move forward to another escape sequence, which is \t, which adds up the tab in the string. Let’s have a look at an example, through which we can understand the \t escape sequence.

As you can see, we have used the escape sequence \t between Gyani and Pandit. So, when we are printing the string, we would simply see that there are some spaces added between Gyani and Pandit.

So, whenever you need to give some tab in the string, you can make use of the \t escape sequence. Again, this is not the thing that you can only use the escape sequence for once. You can use it as many times as you wish, something like this –

I know this does not make sense here, that we are using so many tabs over here, but still, if you want to use it somewhere in the string, you can use it.

Now, Let’s try to use some other escape characters. Let’s say that if we are required to make use of some double quotes, inside our string, we have surrounded our string with the double quotes. So, we would not be able to use the double quotes directly into the program. Have a look at this thing –

As you can see, in the above program, we have surrounded the string with the double quotes, so the double quotes are supposed to mark the start and the end of the string. But here, we wanted the string GyaniPandit into double quotes, so we also surrounded that string GyaniPandit into double quotes. But this didn’t work out in the way that it was supposed to. Instead, we ran into errors.

So, the thing is that we are not directly allowed to use the double quotes, inside a string, which is surrounded by double quotes. In such a case, we are required to use the escape sequence for the same. Have a look at the changes that we have made –

As you can see, now before the double quotes which are surrounding the string GyaniPandit, we have put the backslash, which means that we can now use the double quotes here, and we are not going to run into any errors. So, if you try to run the program, you will simply get to see the string GyaniPandit surrounded with double quotes.

Also, if we do not want to use the escape character here, we can do some alternate things. Like if we know that we are going to use double quotes inside the string, we can surround the string with single quotes. Have a look at the program, which tries to demonstrate the same thing.

As you can see, we could use the double quotes freely, without using the escape sequence, when the string was surrounded by the single quotes. Even the opposite thing is true, which means that if the string is surrounded by double quotes, and you want to use the single quotes in the string, you can simply use the single quotes there. On the other hand, if you are using the single quotes, inside a string, which is surrounded by single quotes, you would have to make use of the escape sequence.

Let’s have a look at an example, to understand this as well.

As you can see, the string in the above program is surrounded by single quotes, and in the string as well, we are trying to use single quotes. We know that we are not allowed to do this directly, so we are making use of the escape sequence (\’). Alternatively, you can surround the string with the double quotes, and then use the single quotes in the string, without any escape sequence.

So, using the escape sequence becomes important sometimes, when we are required to use something that we would not be allowed to use otherwise.

You can also try some other escape sequences as well, from the table. When we get to use them in the programs, we would get comfortable using them and become familiar with some of the different escape sequences. You can also explore some more escape sequences as well, while we would move to our next concept.