Guide
Python
Beginner
4.13 Escape Characters
The full table of Python escape sequences — newline, tab, backslash, quotes, carriage return, and backspace — with examples of each.
Escape sequences let you embed special or hard-to-type characters inside an ordinary quoted string, using a backslash followed by a code.
| Escape | Meaning | Example |
|---|---|---|
\n | Newline | "a\nb" → two lines |
\t | Tab | "a\tb" → "a b" |
\\ | Literal backslash | "a\\b" → a\b |
\' | Literal single quote | 'it\'s' → it's |
\" | Literal double quote | "say \"hi\"" → say "hi" |
\r | Carriage return | used in some line-ending formats |
\b | Backspace | moves cursor back one position |
>>> print("a\nb")
a
b
>>> print("a\tb")
a b
>>> print("say \"hi\"")
say "hi"
The full escape-character reference, including octal/hex codes, is covered in 9.3 Escape Characters.
Quick Interview Answer
“An escape sequence is a backslash followed by a code that represents a special character inside a string —
\nfor newline,\tfor tab,\\for a literal backslash,\'/\"for a quote that would otherwise end the string early.”
Common Mistakes
- Forgetting to escape a quote character that matches the string’s own delimiter, causing the string to end early and raise a
SyntaxError. - Confusing
\n(a two-character escape sequence Python turns into one newline byte) with an actual line break typed into a triple-quoted string — both work, but they’re not the same mechanism.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form