What is escape sequence?

The sequence of character which has indirect meaning when it placed within double quotes.

It will optimize some the repetitive tasks while programming.

Example

While printing some statement, if you want to give horizontal tab (usually four spaces) in between every word like below,


Example

#escape sequence example

print("Happy    New    Year")

Here, we manually gave four space between each word. This can be achieved easily with \t escape sequence.

Like below,


Example

#escape sequence in python

print("Happy\tNew\tYear")

Useful Escape Sequences

Escape sequence Description Example Output
\n New line print("Hello\nWorld") Hello
World
\t Horizontal tab print("Hello\tWorld") Hello    World
\' Single quote print("Hello \'World\' ") Hello 'World'
\" Double quote print("Hello \"World\" ") Hello "World"
\\ Backslash print("Hello \\World") Hello \World