Python - Escape Characters
An escape character is a character followed by a backslash (). It tells the Interpreter that this escape character (sequence) has a special meaning. For instance, \n is an escape sequence that represents a newline. When Python encounters this sequence in a string, it understands that it needs to start a new line.
Unless an ‘r’ or ‘R’ prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. In Python, a string becomes a raw string if it is prefixed with “r” or “R” before the quotation symbols. Hence ‘Hello’ is a normal string whereas r’Hello’ is a raw string.
Example
In the below example, we are practically demonstrating raw and normal string.
# normal string
normal = "Hello"
print (normal)
# raw string
raw = r"Hello"
print (raw)
Output of the above code is shown below −
Hello
Hello
In normal circumstances, there is no difference between the two. However, when the escape character is embedded in the string, the normal string actually interprets the escape sequence, whereas the raw string doesn’t process the escape character.
Example
In the following example, when a normal string is printed the escape character ‘\n’ is processed to introduce a newline. However, because of the raw string operator ‘r’ the effect of escape character is not translated as per its meaning.
normal = "Hello\nWorld"
print (normal)
raw = r"Hello\nWorld"
print (raw)
On running the above code, it will print the following result −
Hello
World
Hello\nWorld
Escape Characters in Python
The following table shows the different escape characters used in Python -
Sr.No | Escape Sequence & Meaning |
---|---|
1 | **<newline>**Backslash and newline ignored |
2 | **\**Backslash () |
3 | **‘**Single quote (‘) |
4 | **“**Double quote (“) |
5 | \aASCII Bell (BEL) |
6 | \bASCII Backspace (BS) |
7 | \fASCII Formfeed (FF) |
8 | \nASCII Linefeed (LF) |
9 | \rASCII Carriage Return (CR) |
10 | \tASCII Horizontal Tab (TAB) |
11 | \vASCII Vertical Tab (VT) |
12 | \oooCharacter with octal value ooo |
13 | \xhhCharacter with hex value hh |
Escape Characters Example
The following code shows the usage of escape sequences listed in the above table −
# ignore \
s = 'This string will not include \
backslashes or newline characters.'
print (s)
# escape backslash
s=s = 'The \\character is called backslash'
print (s)
# escape single quote
s='Hello \'Python\''
print (s)
# escape double quote
s="Hello \"Python\""
print (s)
# escape \b to generate ASCII backspace
s='Hel\blo'
print (s)
# ASCII Bell character
s='Hello\a'
print (s)
# newline
s='Hello\nPython'
print (s)
# Horizontal tab
s='Hello\tPython'
print (s)
# form feed
s= "hello\fworld"
print (s)
# Octal notation
s="\101"
print(s)
# Hexadecimal notation
s="\x41"
print (s)
It will produce the following output −
This string will not include backslashes or newline characters.
The \character is called backslash
Hello 'Python'
Hello "Python"
Helo
Hello
Hello
Python
Hello Python
hello
world
A
A