Escape Characters in Python
In Python, escape characters are special
characters used to insert characters that are illegal or difficult
to type directly in a string. These characters are preceded by a backslash
(\)
.
Escape characters help us include special characters in strings that would otherwise cause errors or be interpreted differently. For example, inserting a new line, tab space, quote inside a quote, etc.
Escape Characters:
| Escape Sequence | Description |
|---|---|
\' |
Inserts a single quote |
\" |
Inserts a double quote |
\\ |
Inserts a backslash |
\n |
Inserts a new line |
\t |
Inserts a tab space |
\b |
Inserts a backspace (moves cursor one step back) |
\r |
Inserts a carriage return (moves cursor to beginning of line) |
\f |
Inserts a form feed (new page character, rarely visible) |
\a |
System alert / bell sound (may not work on all systems) |
\v |
Inserts a vertical tab |
Example:
print("1. Double quote: \"Hello\"")
print('2. Single quote: It\'s Python')
print("3. New Line:\nThis is on a new line")
print("4. Tab Space:\tThis is tabbed")
print("5. Backslash: \\")
print("6. Backspace: ABC\bD")
print("7. Carriage Return: Hello\rStart")
print("8. Form Feed: Line1\fLine2")
print("9. Vertical Tab: Text1\vText2")
print("10. Null Character: End\0Start")
1. Double quote: "Hello"
2. Single quote: It's Python
3. New Line:
This is on a new line
4. Tab Space: This is tabbed
5. Backslash: \
6. Backspace: ABD
7. Carriage Return: Starto
8. Form Feed: Line1 Line2
9. Vertical Tab: Text1 Text2
10. Null Character: EndStart
Explanation:
\"→ Inserts double quotes inside double-quoted string.\'→ Inserts a single quote inside single-quoted string.\n→ Inserts a newline character and moves the text after it to a new line.\t→ Adds a horizontal tab (similar to pressing the Tab key).\\→ Prints a single backslash.\b→ Backspace: Deletes the character just before it.
For example:"ABC\bD"becomes"ABD"because\bdeletes'C'.\r→ Carriage return: Moves the cursor to the beginning of the current line **without a newline**, and the characters after\rstart overwriting from the beginning.
"Hello\rStart"becomes"Starto":
– "Hello" is written first, then\rreturns cursor to start.
– "Start" overwrites "Hello" starting from the beginning → **"Start" + "o"** (from Hello).\f→ Form feed: Inserts a form feed character which is used in printers to move to the next page.
In most terminals, it may appear as a small symbol or space:"Line1\fLine2"appears like a space or unreadable character between lines.\v→ Vertical tab: Inserts vertical space (like a tab, but vertical direction).
Example:"Text1\vText2"– output may look like line break or special spacing depending on the environment.\0→ Null character: Indicates end of string in C/C++, but in Python it is treated as just a character with no visible effect.
"End\0Start"prints as:"EndStart"(with invisible separator).
Summary:
- Escape characters start with a backslash
(
\) and allow special formatting inside strings. - They help include characters like newlines, tabs, quotes, and file paths in a proper way.
- Common ones include
\n,\t,\\,\',\", etc. - Some escape characters like
\rand\bbehave differently depending on the platform and terminal. - Some escape characters (like
\a,\v,\f,\0) may not show visible output in all environments but still exist in the string.