Module 7 · Strings — topic 21 of 22
Escape characters
Backslash escape sequences put special characters into strings: newlines, tabs, quotes and backslashes themselves. Learn the common escapes, Unicode escapes, repr() and raw strings.
Learning objectives
- Use \n, \t, \\ and quote escapes
- Inspect escapes with repr()
- Choose raw strings to avoid unwanted escapes
Definition
Backslash escape sequences put special characters into strings: newlines, tabs, quotes and backslashes themselves. Learn the common escapes, Unicode escapes, repr() and raw strings.
Introduction
Some characters can’t be typed directly inside a string literal, such as a new line or the same quote that surrounds the string. A backslash followed by a code, called an escape sequence, represents them: \n is a new line, \t a tab, \" a double quote.
Why it matters
Formatting output across lines and columns, writing quotes inside text and handling Windows paths all involve escapes. Understanding them also explains mysterious bugs such as a path whose \n became a line break.
Detailed explanation
Escape sequences are processed when Python reads your source code, not when the program prints. By the time the program runs, "a\tb" already contains a real tab character between a and b, which is why len() counts each escape as a single character.
Each escape sequence counts as one character: len("a\nb") is 3.
print shows the effect of escapes; repr() and the interactive shell show the escape codes themselves, which is helpful for spotting hidden characters.
An unknown escape such as \d is left as a backslash and a letter, but Python warns about it (SyntaxWarning: invalid escape sequence). Use raw strings for such text.
Raw strings (r"...") switch off escape processing, so r"C:\new" keeps the backslash and n.
Inside triple-quoted strings you can usually type real line breaks instead of \n.
Syntax
- A backslash starts an escape.
- \\ produces one backslash.
- Escaped quotes don’t end the string.
Code examples
Common escapes
Line 1 Line 2 Name: Asha She said "hi" Back\slash 3
Seeing hidden characters
'total\t42\n' Price: ₹499 ₹
More examples
Tab-separated output and paths
Name Marks City Asha 82 Pune Ben 67 Delhi 'C:\new\table.txt' C:\new\table.txt 16
Step-by-step explanation
- \t separates columns with tab characters, a format spreadsheets can import.
- In broken, \n became a newline and \t a tab; repr() reveals those escape codes.
- The raw string keeps every backslash, so fixed is the real path of 16 characters.
- When printed, the raw path shows exactly as typed.
Expected output
Running “Tab-separated output and paths” prints exactly:
Practical applications
- Exporters write tab-separated files with \t.
- Log formats separate fields with tabs or use \n between entries.
- Windows paths and regular expressions are written as raw strings to avoid escape mistakes.
Best practices
- Use raw strings or forward slashes for Windows paths.
- Use repr() to inspect strings with hidden characters.
- Prefer triple-quoted strings for long multi-line text.
Common mistakes
Unintended escapes in paths
Avoid
Prefer
\t and \n are interpreted as tab and newline.
Ending a string with a single backslash
Avoid
Prefer
Even a raw string can’t end with an odd backslash, because it escapes the closing quote.
Key takeaways
- Escapes start with a backslash: \n, \t, \\, \", \'.
- repr() shows escape codes; print shows their effect.
- Raw strings turn escape processing off.
Try it yourself
- 1Print a two-column table of three fruits and prices separated by tabs.
- 2Print the text: Use \n for a new line (showing the backslash and n literally).
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?