Escape characters in Python are special characters that don't get printed as-is but instead signal some specific action. They allow you to include special characters in strings or control the formatting of text. In this comprehensive guide, we'll explore the world of escape characters in Python and how they can be used to enhance text manipulation.
flowchart TD A["'a\\nb' in source"] --> B["the parser sees an escape"] B --> C["one newline character"] C --> D["len == 3"] E["r'a\\nb'"] --> F["no escape processing"] F --> G["backslash and n, two characters"] G --> H["len == 4"] I["r'C:\\path\\to'"] --> J["the literal Windows path"] K["a raw string ending in a backslash"] --> L["SyntaxError -- the quote is still escaped"]
Basics of Escape Characters
Escape characters are denoted by a backslash (\) followed by a specific character or sequence. The backslash tells Python that the character following it has a special meaning.
Escape Characters in Python
| Sequence | Escape Character | Escape Character Name | Description |
|---|---|---|---|
| 1 | \n | Newline | Inserts a new line |
| 2 | \t | Tab | Inserts a tab |
| 3 | \r | Carriage Return | Inserts a carriage return |
| 4 | \b | Backspace | Inserts a backspace |
| 5 | \f | Form Feed | Inserts a form feed |
| 6 | \v | Vertical Tab | Inserts a vertical tab |
| 7 | \\ | Backslash | Inserts a backslash |
| 8 | \' | Single Quote | Inserts a single quote |
| 9 | \" | Double Quote | Inserts a double quote |
| 10 | \a | Bell | Inserts a bell |
| 11 | \0 | Null Byte | Inserts a null byte |
| 12 | \N{name} | Unicode Character | Inserts a Unicode character |
| 13 | \xhh | Hexadecimal Character | Inserts a hexadecimal character |
| 14 | \uhhhh | Unicode 16-bit Hexadecimal Character | Inserts a Unicode 16-bit hexadecimal character |
| 15 | \Uhhhhhhhh | Unicode 32-bit Hexadecimal Character | Inserts a Unicode 32-bit hexadecimal character |
| 16 | \ooo | Octal Character | Inserts an octal character |
Newline Escape Character
The newline escape character (\n) inserts a new line into a string. It's useful for formatting text and creating line breaks. For example, you can use it to print a string on multiple lines:
# print string on multiple lines
print('This is a string\non multiple lines')Output
C:\Users\Your Name> python strings.py
This is a string
on multiple linesTab Escape Character
The tab escape character (\t) inserts a tab into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a tab:
# print string with tab
print('This is a string\twith a tab')Output
C:\Users\Your Name> python strings.py
This is a string with a tabCarriage Return Escape Character
The carriage return escape character (\r) inserts a carriage return into a string. It's useful for formatting text and creating line breaks. For example, you can use it to print a string on multiple lines:
# print string on multiple lines
print('This is a string\ron multiple lines')Output
C:\Users\Your Name> python strings.py
This is a string
on multiple linesBackspace Escape Character
The backspace escape character (\b) inserts a backspace into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a backspace:
# print string with backspace
print('This is a string\bwith a backspace')Output
C:\Users\Your Name> python strings.py
This is a stringwith a backspaceForm Feed Escape Character
The form feed escape character (\f) inserts a form feed into a string. It's useful for formatting text and creating line breaks. For example, you can use it to print a string on multiple lines:
# print string on multiple lines
print('This is a string\fon multiple lines')Output
C:\Users\Your Name> python strings.py
This is a string
on multiple linesVertical Tab Escape Character
The vertical tab escape character (\v) inserts a vertical tab into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a vertical tab:
# print string with vertical tab
print('This is a string\vwith a vertical tab')Output
C:\Users\Your Name> python strings.py
This is a string
with a vertical tabBackslash Escape Character
The backslash escape character (\\) inserts a backslash into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a backslash:
# print string with backslash
print('This is a string\\with a backslash')Output
C:\Users\Your Name> python strings.py
This is a string\with a backslashSingle Quote Escape Character
The single quote escape character (\') inserts a single quote into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a single quote:
# print string with single quote
print('This is a string\'with a single quote')Output
C:\Users\Your Name> python strings.py
This is a string'with a single quoteDouble Quote Escape Character
The double quote escape character (\") inserts a double quote into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a double quote:
# print string with double quote
print('This is a string\"with a double quote')Output
C:\Users\Your Name> python strings.py
This is a string"with a double quoteBell Escape Character
The bell escape character (\a) inserts a bell into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a bell:
# print string with bell
print('This is a string\awith a bell')Output
C:\Users\Your Name> python strings.py
This is a stringwith a bell Null Byte Escape Character
The null byte escape character (\0) inserts a null byte into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a null byte:
# print string with null byte
print('This is a string\0with a null byte')Output
C:\Users\Your Name> python strings.py
This is a stringwith a null byteUnicode Character Escape Character
The Unicode character escape character (\N{name}) inserts a Unicode character into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a Unicode character:
# print string with Unicode character
print('This is a string\N{COPYRIGHT SIGN}with a Unicode character')Output
C:\Users\Your Name> python strings.py
This is a string©with a Unicode characterHexadecimal Character Escape Character
The hexadecimal character escape character (\xhh) inserts a hexadecimal character into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a hexadecimal character:
# print string with hexadecimal character
print('This is a string\x24with a hexadecimal character')Output
C:\Users\Your Name> python strings.py
This is a string$with a hexadecimal characterUnicode 16-bit Hexadecimal Character Escape Character
The Unicode 16-bit hexadecimal character escape character (\uhhhh) inserts a Unicode 16-bit hexadecimal character into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a Unicode 16-bit hexadecimal character:
# print string with Unicode 16-bit hexadecimal character
print('This is a string\u0024with a Unicode 16-bit hexadecimal character')Output
C:\Users\Your Name> python strings.py
This is a string$with a Unicode 16-bit hexadecimal characterUnicode 32-bit Hexadecimal Character Escape Character
The Unicode 32-bit hexadecimal character escape character (\Uhhhhhhhh) inserts a Unicode 32-bit hexadecimal character into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a Unicode 32-bit hexadecimal character:
# print string with Unicode 32-bit hexadecimal character
print('This is a string\U00000024with a Unicode 32-bit hexadecimal character')Output
C:\Users\Your Name> python strings.py
This is a string$with a Unicode 32-bit hexadecimal characterOctal Character Escape Character
The octal character escape character (\ooo) inserts an octal character into a string. It's useful for formatting text and creating tabbed sections. For example, you can use it to print a string with an octal character:
# print string with octal character
print('This is a string\044with an octal character')Output
C:\Users\Your Name> python strings.py
This is a string$with an octal characterConclusion
Escape characters in Python are powerful tools for manipulating text, controlling formatting, and handling special characters. Understanding how to use escape sequences enhances your ability to work with strings and create well-formatted, readable code.
As you continue your Python journey, experiment with different escape characters, explore their applications, and incorporate them into your projects. Whether you're working with strings, handling special characters, or managing formatting, escape characters are valuable assets in your programming toolbox.
For more tips, tricks, and practical examples, check out our tutorials on Python Central Hub!
Check yourself
What is `len('a b')` and what is `len(r'a b')`?
Verified. In the ordinary literal the parser turns backslash-n into one newline character; the raw string keeps both characters.
Show answer
C — 3 and 4 — Verified. In the ordinary literal the parser turns backslash-n into one newline character; the raw string keeps both characters.
Why are regular expressions usually written as raw strings?
Without a raw string you would have to double every backslash — once for Python's parser and once for the regex engine — which is where the classic four-backslash confusion comes from.
Show answer
B — So the backslash reaches the regex engine instead of being consumed by Python — Without a raw string you would have to double every backslash — once for Python's parser and once for the regex engine — which is where the classic four-backslash confusion comes from.
What does `r'C:'` do?
Verified. Even in a raw string the backslash still escapes the closing quote for the tokenizer, so the literal never terminates.
Show answer
B — SyntaxError — a raw string cannot end with a backslash — Verified. Even in a raw string the backslash still escapes the closing quote for the tokenizer, so the literal never terminates.
Does a raw string change the VALUE stored, or only how it is written?
`r'...'` is purely a parsing instruction. The object produced is a normal `str` with no memory of how it was spelled.
Show answer
B — Only how it is written — the result is an ordinary `str` — `r'...'` is purely a parsing instruction. The object produced is a normal `str` with no memory of how it was spelled.
Try it: Escape Character Exercises
Exercise 1 – Newline and Tab
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Quotes Inside Strings
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Raw String
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.