Python Escape Characters


Python Escape Characters

There are some characters that have a special meaning when used in a string. But what do yo do if you would like to insert that character in the string as is, without invoking its special meaning.

For understanding this, let us take a simple example. We use single quotes or double quotes to define a string. Suppose, we define a string with single quotes. The first occurrence of single quote marks the start of string and the second occurrence marks the end of the string. Now, consider that we would like to have a single quote in our string. What do we do now. If we place a single quote just like that in the middle of string, Python would think that this is the end of the string, which is actually not.

To insert these kind of illegal characters, we need the help of a special character like backslash \.

Escape Characters

Following table provides the list of escape characters in Python.

CodeDescription
\'Single Quote
\"Double Quote
\\Backslash
\nNew Line
\rCarriage Retrun
\tTab
\bBackspace
\fForm Feed
\oooOctal Value
\xhhHex Value

Single Quote Escape Character

To escape single quote character, use a preceding backslash for the single quote in the string.

Python Program

x = 'hello\'world'
print(x)

Output

hello'world

If you are using double quotes to define a string, you may not use the escape sequence to escape the single quote. But, even if you use, that does not change the output, anyways.

Python Program

x = "hello'world"
print(x)

Output

hello'world

Double Quote Escape Character

To escape double quote character, use a preceding backslash for the double quote in the string.

Python Program

x = "hello\"world"
print(x)

Output

hello"world

If you are using single quotes to define a string, you may not use the escape sequence to escape the double quote. But, even if you use, that does not change the output.

Python Program

x = 'hello"world'
print(x)

Output

hello"world

Backslash Escape Character

To escape backslash character, use a preceding backslash for backslash in the string. That would look like two backslashes in the string.

Python Program

x = 'hello\\world'
print(x)

Output

hello\world

Newline Escape Character

To escape newline character, use a preceding backslash for character 'n' in the string.

Python Program

x = 'hello\nworld'
print(x)

Output

hello
world

Carriage Return Escape Character

To escape carriage return character, use a preceding backslash for character 'r' in the string.

Python Program

x = 'hello\rworld'
print(x)

Output

world

After printing hello, carriage return will take the cursor to the start of the same line, and then it prints world, which kind of overwrites on the previous data. So, you see only world, but no hello, in the output.

Tab Escape Character

To escape tab character, use a preceding backslash for character 't' in the string.

Python Program

x = 'hello\tworld'
print(x)

Output

hello   world

Backspace Escape Character

To escape backspace character, use a preceding backslash for character 'b' in the string.

Python Program

x = 'hello\bworld'
print(x)

Output

hellworld

After printing hello, a backspace would delete the last character o, and then world is printed out. So, the final result would look like hellworld.

Form feed Escape Character

To escape form feed character, use a preceding backslash for character 'f' in the string.

Python Program

x = 'hello\fworld'
print(x)

Output

hello
     world

Octal Value Escape Character

To escape a byte of octal value character, use a preceding backslash for three digit octal value in the string.

Python Program

x = '\101\102\103'
print(x)

Output

ABC

Octal value 101 represents A, 102 represents B, and so on. So, in the output, we got ABC for the given octal values.

Hex Value Escape Character

To specify a byte using hex value, use a preceding backslash and x for two digit hex value in the string.

Python Program

x = '\x41\x42\x43'
print(x)

Output

ABC

Hex value of 41 means 65 in decimal. And a byte with decimal 65 represents the character A. Similarly 42 is B, 43 is C.

Summary

In this tutorial of Python Examples, we learned what escape characters are, how to use them in a string, and their usage, with the help of example programs for each of the escape characters.