Escape Sequence and Escape Characters

Escape Sequence and Escape Characters

What is an Escape Sequence?

In Python, escape sequences are indicated by a backslash ( \ ). The most important one may be \n which indicates a new line. Like so, multiple logical lines can be stacked into a single physical line. Another escape sequence worth mentioning is \' for a single quote within a string.

Escape Character

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a new line, and "\r" is a carriage return. An escape character is a backslash \ followed by the character you want to insert.

Table for Escape Characters

Let's discuss some of the Escape Characters Used in Python. There is a table below containing different Escape Characters:-

Code Result
\' Single Quote
\\ Backlash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f From Feed
\ooo Octal Value
\xhh Hex value
\a ASCII Bell
\v ASCII Vertical Tab

Using \'

So starting with \' here we will see the use of Single Quote Escape Character.

Using \\

Here we will understand the use of the Backslash Escape Character.

We use Backslash Character to get a backslash in our output.

Using \n

The use of \n is to add a new line which is the most used Escape Character.

So in the above code, I had used \n to separate two lines according to the statement. It is the best character used to separate two lines.

Using \r

\r is the escape Character used for a return statement

\r is used to return a single statement in a code.

Using \t

If you want to add a spacing of tab then you should use \t Escape Character.

As you can see from the above code that there is a spacing of tab between World and How. We use \t when we are creating projects and games.

Using \b

\b is used to add a backspace in a code which is also an Escape Character.

So as you can see from the above code we had used \b which is used for deleting the last written word which is also known as backspace. So \b is used after are then you can clearly see that e is missing from are.

Using \ooo

The Octal Escape Character ends when it either contains three octal digits already, or the next character is not an octal digit.

So in the above code, I had used an octal code which is used to print Hello.

Using \xhh

\xhh is just a notation to help you create the values; when echoing a byte string Python will always display printable ASCII characters directly rather than use the \xhh notation.

So in the above code, I had used a hex code that will give the output the same as the octal escape character and the output is Hello.

For more help regarding this topic, visit my channel and start learning python from basics and become a python coder. Also, make sure to follow my page on Instagram. I provide weekly quizzes, upload memes, some useful codes, and even some entertaining reels that will make you love my page.

Enjoy learning. Thanks for reading. Stay tuned with us and also subscribe to our newsletter.

NOTE:- The awesome artwork used in this article was created by Product Management.