In Python, escape characters are used when we need to include special characters in a string that are otherwise hard (or illegal) to type directly. These are preceded by a backslash (\), which tells Python that the next character is going to be a special character. They’re especially helpful for:
- Formatting strings (adding tabs, newlines)
- Including quotes inside quotes
- Writing file paths
- Inserting control characters
Here’s a breakdown of commonly used escape characters in Python:
Escape Character | Description |
---|
\n | Newline – Moves the cursor to the next line. |
---|
\t | Tab – Adds a horizontal tab. |
---|
\\ | Backslash – Inserts a literal backslash. |
---|
\' | Single Quote – Inserts a single quote inside a single-quoted string. |
---|
\" | Double Quote – Inserts a double quote inside a double-quoted string. |
---|
\r | Carriage Return – Moves the cursor to the beginning of the line. |
---|
\b | Backspace – Moves the cursor one position back, effectively deleting the last character. |
---|
\f | Form Feed – Moves the cursor to the next page. |
---|
\v | Vertical Tab – Moves the cursor vertically. |
---|
\xhh | Hexadecimal – Represents a character using hexadecimal value hh. |
---|
Detailed Explanation with Examples
1. \n (Newline)
Breaks the string into a new line.
Python
print("Hello, World!\nWelcome to Python.")
OutputHello, World!
Welcome to Python.
2. \t (Tab)
The \t escape character inserts a tab space between words or characters.
Python
print("Name\tAge\tLocation")
3. \\ (Backslash)
The \\ escape character inserts a literal backslash in the string.
Python
print("This is a backslash: \\")
OutputThis is a backslash: \
4. \' (Single Quote)
The \' escape character allows you to insert a single quote within a string that is enclosed by single quotes.
Python
print('It\'s a great day!')
5. \" (Double Quote)
The \" escape character allows you to insert a double quote within a string that is enclosed by double quotes.
Python
print("He said, \"Hello!\"")
6. \r (Carriage Return)
The \r escape character moves the cursor to the beginning of the line. It can overwrite the existing text.
Python
print("Hello, World!\rHi")
7. \b (Backspace)
The \b escape character moves the cursor one character back, effectively deleting the last character.
Python
The \f escape character moves the cursor to the next page (less commonly used in modern programming).
Python
9. \v (Vertical Tab)
The \v escape character moves the cursor vertically (less commonly used).
Python
10. \xhh (Hexadecimal)
The \xhh escape character represents a character using its hexadecimal value hh.
Python
# Define a string with hexadecimal escape sequences
print("Hello \x48\x65\x6C\x6C\x6F")
Similar Reads
Python | Character Encoding Finding the text which is having nonstandard character encoding is a very common step to perform in text processing. All the text would have been from utf-8 or ASCII encoding ideally but this might not be the case always. So, in such cases when the encoding is not known, such non-encoded text has to
2 min read
Ways to Print Escape Characters in Python In Python, escape characters like \n (newline) and \t (tab) are used for formatting, with \n moving text to a new line and \t adding a tab space. By default, Python interprets these sequences, so I\nLove\tPython will display "Love" on a new line and a tab before "Python." However, if you want to dis
2 min read
Find Frequency of Characters in Python In this article, we will explore various methods to count the frequency of characters in a given string. One simple method to count frequency of each character is by using a dictionary.Using DictionaryThe idea is to traverse through each character in the string and keep a count of how many times it
2 min read
Find Frequency of Characters in Python In this article, we will explore various methods to count the frequency of characters in a given string. One simple method to count frequency of each character is by using a dictionary.Using DictionaryThe idea is to traverse through each character in the string and keep a count of how many times it
2 min read
Python CheatSheet (2025) Python is one of the most widely-used and popular programming languages, was developed by Guido van Rossum and released first in 1991. Python is a free and open-source language with a very simple and clean syntax which makes it easy for developers to learn Python. It supports object-oriented program
15+ min read
Learn Python Basics âPython is a versatile, high-level programming language known for its readability and simplicity. Whether you're a beginner or an experienced developer, Python offers a wide range of functionalities that make it a popular choice in various domains such as web development, data science, artificial in
9 min read