Python Escape Characters


What are Escape Characters in Python?

An escape character is a special character in Python that allows you to use characters that would otherwise have a different meaning in a string or are not printable. Escape characters begin with a backslash (\), followed by a character that defines the special meaning.

Common Escape Characters in Python:

Escape Sequence Description Example Output
\n Newline (line break) Hello\nWorld! prints as:
Hello
World!
\t Horizontal tab Hello\tWorld! prints as: Hello World!
\\ Backslash \\ prints as: \
\' Single quote \' prints as: '
\" Double quote \" prints as: "
\r Carriage return (moves to the beginning of the line) Hello\rWorld! prints as: World!
\b Backspace (removes the previous character) Hello\bWorld! prints as: HellWorld!
\f Form feed (used to simulate a page break in text) Hello\fWorld! prints with a page break between Hello and World!
\u or \U Unicode character (represented by a four-character or eight-character hex value) \u0048 prints as: H
\x Hexadecimal character (2 digits) \x48 prints as: H

Examples of Using Escape Characters


1. Newline (\n) - Example 1

The newline escape character (\n) is used to insert a line break in a string.

text = "Hello\nWorld!"
print(text)

Output:

Hello
World!

In this example, the \n causes the string to break into two lines, printing Hello on one line and World! on the next.


2. Tab (\t) - Example 2

The tab escape character (\t) adds a horizontal tab to the string.

text = "Hello\tWorld!"
print(text)

Output:

Hello    World!

Here, the \t adds a tab space between Hello and World!.


3. Backslash (\\) - Example 3

A backslash is used as an escape character, so to include an actual backslash in the string, you need to use \\.

text = "C:\\Users\\Admin\\Documents"
print(text)

Output:

C:\Users\Admin\Documents

The \\ represents a single backslash, allowing you to display paths in a readable format.


4. Single and Double Quotes (\' and \") - Example 4

If you need to include quotes inside a string that is already enclosed by the same type of quotes, you can use the escape characters for single and double quotes.

Example 1: Single Quote

text = 'It\'s a beautiful day!'
print(text)

Output:

It's a beautiful day!

In this case, the escape character \' is used to insert the single quote within the string.


Example 2: Double Quote

text = "She said, \"Hello!\""
print(text)

Output:

She said, "Hello!"

Here, \" is used to include double quotes within the string.


5. Carriage Return (\r) - Example 5

The carriage return escape character (\r) moves the cursor back to the beginning of the line, effectively overwriting the existing text.

text = "Hello\rWorld!"
print(text)

Output:

World!

In this example, the \r causes World! to overwrite Hello, printing only World!.


6. Backspace (\b) - Example 6

The backspace escape character (\b) deletes the last character.

text = "Hello\bWorld!"
print(text)

Output:

HellWorld!

Here, \b removes the last character (o), so the output is HellWorld!.


7. Unicode (\u and \U) - Example 7

Unicode escape sequences are used to insert characters from the Unicode character set using their hexadecimal code points.


Example 1 (4-digit Unicode):

text = "\u0048\u0065\u006C\u006C\u006F"
print(text)

Output:

Hello

The sequence \u0048 represents the letter H, \u0065 represents e, and so on. The output is the string "Hello".


Example 2 (8-digit Unicode):

text = "\U0001F600"
print(text)

Output:

😀

This example uses an 8-digit Unicode code to insert a smiling face emoji.


8. Hexadecimal (\x) - Example 8

The \x escape character allows you to include characters based on their hexadecimal values.

text = "\x48\x65\x6C\x6C\x6F"
print(text)

Output:

Hello

In this case, \x48 represents H, \x65 represents e, and so on, producing the string "Hello".


Key Points About Escape Characters in Python

  • Escape sequences begin with a backslash (\).
  • Common uses include newline (\n), tab (\t), backslash (\\), and quotes inside strings (\', \").
  • Escape characters can also be used for special characters like Unicode (\u) and hexadecimal (\x).
  • Escape characters are essential when handling strings with special symbols that would otherwise break the syntax or produce unwanted results.