Escape characters in Python let you include special characters inside strings that would otherwise end the string or be hard to type directly. They start with a backslash \ followed by another character, like \n for a new line or \t for a tab.
\ and gives special meaning to the next character.r"...") treat backslashes as normal characters.\n – New line\t – Horizontal tab\\ – Backslash\' – Single quote\" – Double quoteIn Python, strings are usually written inside single ('...') or double ("...") quotes. Escape sequences are written inside these strings:
"Some text\nMore text"\n is interpreted as a newline, \t as a tab.\' inside single-quoted strings, \" inside double-quoted strings.r to stop Python from treating \ as an escape starter.
print("Hello\nWorld") # New line
print("Column1\tColumn2") # Tab
print("Backslash: \\") # Backslash
print('It\'s Python') # Single quote
print("She said, \"Hi\"") # Double quote
Raw strings treat backslashes as literal characters. They are especially useful for Windows file paths and regular expressions, where backslashes appear frequently.
path = r"C:\Users\Alice\Documents"
print(path) # C:\Users\Alice\Documents
regex = r"\d+\s\w+"
print(regex) # \d+\s\w+
You can combine multiple escape characters to format multi-line output neatly:
print("Name:\tAlice\nAge:\t25\nCity:\tNew York")
For: print("Hello\nWorld")
Hello World
For: print("Column1\tColumn2")
Column1 Column2
(There is a tab space between Column1 and Column2.)
For raw path: print(r"C:\Users\Alice\Documents")
C:\Users\Alice\Documents
Without a raw string, some combinations like \U or \t might be treated as escape sequences and cause unexpected behavior.
r"...") for Windows file paths to avoid having to double every backslash.\n and \t to make console output clean and readable.'It\'s Python' ✅"She said, \"Hi\"" ✅"C:\Users\Alice" – they may be treated as escape sequences.r"C:\Users\") – that is invalid in Python.\n, for example:Name, Street, City, Country each on a new line.\t, like:Item Price, each on separate lines with tabs.r"C:\Program Files\Python") and print it.It's called "Python".