In Python, strings are enclosed in single quotes (‘) or double quotes (“). However, sometimes you want to include the quote itself within the string (like “It’s”), or insert special control characters like newlines or tabs.
Writing them directly will either cause a SyntaxError or display incorrectly.
# This results in a SyntaxError
# text = 'It's a syntax error.'
The solution is “Escape Sequences.” This article explains the basics of escape sequences using the backslash (\).
What is an Escape Sequence?
An escape sequence is a combination of a backslash (\) and a following character (or characters) to represent a special meaning.
The backslash tells the Python interpreter: “Interpret the next character not as a normal character, but with a special meaning.”
Common Escape Sequences
Here are the primary escape sequences frequently used in Python.
| Sequence | Meaning |
\' | Single Quote |
\" | Double Quote |
\\ | Backslash itself |
\n | Newline (Line Feed) |
\t | Horizontal Tab |
\r | Carriage Return |
Usage Examples
1. Escaping Quote Characters (\', \")
This is mandatory when you use the same type of quote inside the string as the one defining it.
# 1. Using ' inside single quotes
text_single = 'It\'s a fine day.'
print(text_single)
# 2. Using " inside double quotes
text_double = "He said, \"OK, let's go.\""
print(text_double)
# 3. Mixing quotes (Often more readable)
text_mixed = "It's a \"fine\" day."
print(text_mixed)
Output:
It's a fine day.
He said, "OK, let's go."
It's a "fine" day.
2. Special Control Characters (\n, \t)
\n (newline) and \t (tab) are often used to format output.
# Format header and data with tabs
header = "ProductID\tName\tPrice"
row1 = "A-001\t\tApple\t150"
row2 = "B-002\t\tBanana\t100"
# Separate lines with \n
report = header + "\n" + row1 + "\n" + row2
print(report)
Output:
ProductID Name Price
A-001 Apple 150
B-002 Banana 100
3. Backslash Itself (\\)
If you want to include the backslash character itself in a string, such as in Windows file paths, you must write it twice: \\.
# Want to represent the path: C:\Users\Default
file_path = "C:\\Users\\Default\\Documents"
print(file_path)
Output:
C:\Users\Default\Documents
Raw Strings (r"..."): Disabling Escapes
When handling strings with many backslashes, like Windows paths or regular expressions, writing \\ repeatedly is tedious.
In such cases, you can place r right before the starting quote to treat it as a Raw String. Inside a raw string, backslashes lose their special meaning and are treated as literal characters.
# Using Raw String (r"...")
file_path_raw = r"C:\Users\Default\Documents"
print(file_path_raw)
Output:
C:\Users\Default\Documents
This improves readability because you can write the path exactly as it looks without doubling the backslashes.
Note: Line Continuation (\)
Although similar to escape sequences, the Line Continuation character is a different feature.
If a line of code is too long, placing a backslash \ at the end allows the code to continue on the next line.
# When a single line of code is too long
long_message = "This is a very long text " \
"that continues to the next line in the source code."
print(long_message)
Output:
This is a very long text that continues to the next line in the source code.
Note that this \ and the line break in the source code are not included in the final string value. This is different from \n (newline inside the string).
Summary
- Use the backslash (
\) to represent special characters like\n(newline) and\t(tab), or to escape quotes like\'and\". - To use a backslash as a character, write it as
\\. - Use Raw Strings (
r"...") to disable escape sequences, which is convenient for Windows paths. - A
\at the end of a line is used to continue the code on the next line, not to add a newline to the string.
