Escape Characters in Strings in Python
An escape sequence lets you include something in a string that would otherwise be impossible to type directly — a line break, a tab, or a quote character that would otherwise end the string early. Every escape sequence starts with a backslash followed by a character that has a special meaning: a backslash followed by the letter n, for instance, means "insert a line break here."
Because the backslash itself introduces an escape sequence, getting a single literal backslash into a string takes two backslashes in a row — and for strings that are full of backslashes on purpose, like Windows file paths, Python offers raw strings as an escape hatch from escaping altogether.
The newline sequence
A backslash followed by n breaks the output onto a new line at that exact point.
The tab sequence
A backslash followed by t inserts a horizontal tab, useful for lining up simple columns of text.
The literal backslash sequence
Two backslashes in a row produce a single literal backslash character in the result — necessary because one backslash alone always starts an escape sequence.
Escaped quotes
A backslash before a quote character lets that quote appear inside a string without ending it early, even when it matches the string's own surrounding quote style.
Raw strings turn escaping off
Prefixing a string with the letter r disables escape-sequence processing entirely, so every backslash inside it is treated as an ordinary character — the standard trick for Windows file paths.
print("Line one\nLine two")print("Name:\tAda")print("She said \"hi\"")print(r"C:\new_folder")Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.