Strings
Escape Sequences
Special characters in strings
Interview: String handling
Last Updated: June 12, 2026
•
5 min read
Escape sequences let you include special characters in strings using a backslash (\) prefix. Understanding escape sequences is essential for working with file paths, regex patterns, and formatted output.
Common Escape Sequences
\n— newline\t— tab\\— literal backslash\'/\"— quote characters\r— carriage return\0— null character\xHH— hex value (e.g.,\x41= 'A')\uHHHH— Unicode 16-bit (e.g.,\u00e9= 'é')\UHHHHHHHH— Unicode 32-bit
Avoiding Escape Issues
- Use raw strings (
r"...") to treat backslashes literally - Use forward slashes in file paths:
"C:/Users/name"works on Windows too - Use
pathlib.Pathfor cross-platform file paths
Use Cases
Formatting console output with newlines and tabs
Including special characters and Unicode in strings
Working with regex patterns that contain backslashes
Building cross-platform file paths
Common Mistakes
Windows paths with backslashes triggering unintended escape sequences
Forgetting to use raw strings for regex patterns
Confusing \n (newline) with \r (carriage return) behavior across OS
Not knowing about Unicode escape syntax for international characters