The backslash character ( \ ) is used to escape character. It converts difficult-to-type characters into a string. Let us understand the concept of escaping characters with an example. Suppose, we want to print a string with double quotes or single quotes. Usually, when we use single or double quotes with string, Python neglects them and prints only the string. What should we do if we want the quotation mark in the output? Then we will need to make use of the escaping character concept.
print("I am 5'5\" tall.")
#Output
I am 5'5" tall.
Note that we use a backslash operator befor the 2nd double quotation marks. As a result Python interpreter understands that the quotation mark are a part of the string and should be displayed in output. Following Table shows various common escape characters
Sr. No. | Escape Sequence | Meaning |
1 | \newline | Ignored |
2 | \ \ | Backslash (\ ) |
3 | \' | Single quote (' ) |
4 | \" | Double quote (" ) |
5 | \a | ASCII Bell (BEL) |
6 | \b | ASCII Backspace (BS) |
7 | \f | ASCII Formfeed (FF) |
8 | \n | ASCII Linefeed (LF) |
9 | \r | ASCII Carriage Return (CR |
10 | \t | ASCII Horizontal Tab (TAB) |
11 | \v | ASCII Vertical Tab (VT) |
12 | \ooo | Character with octal value ooo |
13 | \xhh... | Character with hex value hh |