Learn Python
>>> variables_data_types.escape_characters

Escape Characters in Strings in Python

#concept

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.

#how it works
1

The newline sequence

A backslash followed by n breaks the output onto a new line at that exact point.

2

The tab sequence

A backslash followed by t inserts a horizontal tab, useful for lining up simple columns of text.

3

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.

4

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.

5

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.

#example
print("Line one\nLine two")
print("Name:\tAda")
print("She said \"hi\"")
print(r"C:\new_folder")
Output
Line one
Line two
Name: Ada
She said "hi"
C:\new_folder
1The first print() call contains a backslash followed by n in the middle of the string, which breaks the output onto two separate lines when it runs.
2The second call contains a backslash followed by t, which inserts a tab character between 'Name:' and 'Ada'.
3The third call uses a backslash before each double-quote character, letting a literal quote appear inside a string that's itself delimited by double quotes, without ending it early.
4The fourth string is prefixed with r, marking it as a raw string — Python treats every backslash inside it as an ordinary character rather than the start of an escape sequence, which is exactly why Windows-style paths are usually written this way.
#use cases
Formatting multi-line output or simple reports without multiple print() calls
Including quote characters inside a string without switching quote styles
Writing Windows file paths as raw strings, avoiding every backslash being misread as the start of an escape sequence
Building simple tab-aligned columns for console output

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.