Escape Sequence in Python with Examples

An escape sequence in Python is a group or sequence of characters that does not represent its own literal meaning when used inside a character or string.

It starts with a single backslash (\) followed by some characters that have some specific tasks to do.

In Python, the backslash (\) is known as escape character. It tells the Python interpreter to interpret the next character(s) in a specific way and ignores its usual meaning.

We generally use an escape sequence to control printed or displayed output. Let’s understand it with the help of an example.

Suppose we want to display a message with a single quotation mark in the output. If we write it directly, an error occurs:

print('He said 'Python is fun unlimited'')
Output:
      File "C:\Python Project\EscapeSequence.py", line 1
      print('He said 'Python is fun unlimited'')
          ^^^^^^^^^^^^^^^^^^^^^^^
      SyntaxError: invalid syntax.

As you can see, the interpreter generates a SyntaxError: invalid syntax. When the Python interpreter encounters the second quote, then it will think that the second quote is the end of the string and does not know what to do with the rest of the characters that come after the second quote.

One simple method to fix it is to use double quotation marks around the string, like this:

print("He said 'Python is fun unlimited'")
Output:
     He said 'Python is fun unlimited'

However, we can also use a single quotation mark around a string containing double quotation marks.

print('He said "Python is fun unlimited"')

If you need to put a double quotation marks in a string literal, you can easily apply single quotation marks around the string. But if you need to use both kinds of quotation marks in a string? In this case, we can do like this:

print("He said \"Python is fun unlimited\"")

In this example, we have used an escape sequence that is a combination of the backslash and the double quote.

The interpreter interprets the combination of two symbols in the escape sequence as a whole, rather than individually. The symbols \ and ” together represent an escape sequence, but those two symbols represent a single character.

Python Escape Sequences in Table


Here, we have listed all valid escape sequences used in Python program that are in the below table:

Escape SequenceDefinition
\’Single quote
\”Double quote
\tTab
\nLine feed (Newline)
\\Backslash
\rCarriage return
\aASCII bell
\oooRepresents an octal
\xhhRepresents the hexadecimal number
\bASCII backspace
\fform feed
\sSpace
\vVertical tab
\N{name}A character name in the Unicode database
\uxxxxA character with 16-bit hex value
\uxxxxxxxxA character with 32-bit hex value

Example Program based on Escape Sequence Characters


Let’s take some important example programs based on Python escape sequence characters.

a) Program based on the escape sequence \’:

# \': Used for printing the text string with a single quotation mark.
print('Good Morning \'Scientech Easy\'')
Output:
      Good Morning 'Scientech Easy'

b) Program based on escape sequence \”:

# \": Used for printing the text string with double quotation marks.
print("Welcome to \"Scientech Easy\"")
Output:
      Welcome to "Scientech Easy"

c) Program based on the escape sequence \t:

# \t: It gives a tab between two words.
print("Welcome to\tScientech Easy")
Output:
      Welcome to Scientech Easy

d) Program based on escape sequence character \n:

# \n: This escape sequence breaks the statement line into two lines.
print("Good Morning \nScientech Easy")
print("*\n**\n***\n")
Output:
       Good Morning 
       Scientech Easy
       *
       **
       ***

e) Program based on escape sequence \\:

# \\: It prints the text string with backslash.
print('Good Evening \\Scientech Easy\\')
Output:
      Good Evening \Scientech Easy\

f) Program based on escape sequence \r:

# \r: This escape sequence moves the output \
# point back to the beginning of the line without moving down a line (generally).
print('Good Morning \rScientech Easy')
Output:
      Scientech Easy

g) Suppose you are working in an MNC as Sr. Software Programmer. Write a Python program to print your profile using suitable escape characters.

emp_code = "IT-35264"
name = "Ivaan Sagar"
desig = "Sr. Software Programmer"
comp = "TCS"
salary = "USD 40000"

print("Employee code: ", emp_code, "\nName: ", name)
print("Designation: ",desig, "\tCompany: ", comp)
print("Annual Salary: ", salary)
Output:
      Employee code:  IT-35264 
      Name:  Ivaan Sagar
      Designation:  Sr. Software Programmer 	Company:  TCS
      Annual Salary:  USD 40000

h) Python program based on all important escape sequence characters:

print("1. Hello \'Python")
print("2. Hello \"Python")
print("3. Hello \tPython")
print("4. Hello \nPython")
print("5. Hello \rPython")
print("6. Hello \a Python")
print("7. Hello \\Python")
print("8. \ooo 20")
print("9. Hello \nPython")
print("10. Hello \bPython")
print("11. Hello \fPython")
print("12. Hello \vPython")
Output: 
      1. Hello 'Python
      2. Hello "Python
      3. Hello  Python
      4. Hello 
      Python
      Python
      6. Hello  Python
      7. Hello \Python
      8. \ooo 20
      9. Hello 
      Python
      10. HelloPython
      11. Hello Python
      12. Hello Python

In this tutorial, we have discussed escape sequence in Python with various example programs. Hope that you will have understood how to use the escape sequence characters to format printed or displayed output.
Thanks for reading!!!
Next ⇒ Python variables⇐ Prev Next ⇒