Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Escape Sequences – Special Characters

Escape sequences in Python are used to insert special characters into strings that are otherwise difficult to include directly. These sequences start with a backslash (\) followed by a specific character. Escape sequences enable the inclusion of characters such as newlines, tabs, and quotes within strings, allowing for more control over string formatting and display.

Escape Sequences in Python

Table of Escape Sequences

Escape SequenceDescription
\\Backslash
\'Single quote
\"Double quote
\nNewline
\tHorizontal tab
\rCarriage return
\bBackspace
\fForm feed
\aBell/alert
\vVertical tab
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh
\N{name}Character from Unicode database with name name
\uXXXXCharacter with 16-bit hex value XXXX
\UXXXXXXXXCharacter with 32-bit hex value XXXXXXXX
\ followed by a newlineContinuation of a string literal over multiple lines

Example 1: Using Escape Sequences for Formatting

# Example 1: Using escape sequences for formatting
name = "Alice"
age = 30

# Using escape sequences to format the output
print(f"Hello, {name}! You are {age} years old.")
print(f"Hello, {name}! You are {age} years old.\n")  # \n adds a newline after the string

Explanation:

  • In this example, we use an f-string (formatted string literal) which allows embedding expressions inside string literals using curly braces {}.
  • "\n" is the escape sequence for a newline character. It inserts a line break in the output.
  • The first print statement outputs: Hello, Alice! You are 30 years old. on one line.
  • The second print statement outputs the same string but with a newline character at the end, resulting in the next output starting on a new line.

Example 2: Using Escape Sequences for Special Characters

# Example 2: Using escape sequences for special characters
path = "C:\\Users\\Alice\\Documents\\file.txt"

# Displaying the path using escape sequences
print(f"The file is located at: {path}")

Explanation:

  • In this example, the string path contains a Windows file path.
  • "\\" is the escape sequence for a backslash character \. In strings, a single backslash \ is used to escape special characters, so to represent a literal backslash in a string, you need to use \\.
  • The output of print(f"The file is located at: {path}") will be: The file is located at: C:\Users\Alice\Documents\file.txt.

Example 3: Using Escape Sequences for Formatting

# Example: Using 5 different escape sequences
message = "Hello,\n\tPython!\nI hope you're learning a lot.\n\nThis is a backslash: \\\nAnd this is a double quote: \""

print(message)

Explanation:

  • \n is used for a newline character. It moves the cursor to the next line.
  • \t is used for a tab character. It adds a tab space.
  • \\\ is used to insert a literal backslash \. In strings, a single backslash \ is used to escape special characters, so \\ is necessary to display a single backslash.
  • \" is used to insert a double quote character " within a string that is already enclosed in double quotes. This escapes the character to prevent the string from being prematurely terminated.

Output: When you run the code, the output will be:

Hello,
    Python!
I hope you're learning a lot.

This is a backslash: \
And this is a double quote: "

In this example:

  • The string message contains multiple lines of text separated by \n for newlines and \t for tabs.
  • It also includes examples of \\ to display a single backslash and \" to include double quotes within a string enclosed by double quotes.