Skip to content

Module 7 · Strings — topic 21 of 22

Escape characters

Backslash escape sequences put special characters into strings: newlines, tabs, quotes and backslashes themselves. Learn the common escapes, Unicode escapes, repr() and raw strings.

Beginner 10 min read·Lesson 102 of 306 in Python Programming

Learning objectives

  • Use \n, \t, \\ and quote escapes
  • Inspect escapes with repr()
  • Choose raw strings to avoid unwanted escapes

Definition

Backslash escape sequences put special characters into strings: newlines, tabs, quotes and backslashes themselves. Learn the common escapes, Unicode escapes, repr() and raw strings.

Introduction

Some characters can’t be typed directly inside a string literal, such as a new line or the same quote that surrounds the string. A backslash followed by a code, called an escape sequence, represents them: \n is a new line, \t a tab, \" a double quote.

Why it matters

Formatting output across lines and columns, writing quotes inside text and handling Windows paths all involve escapes. Understanding them also explains mysterious bugs such as a path whose \n became a line break.

Detailed explanation

Escape sequences are processed when Python reads your source code, not when the program prints. By the time the program runs, "a\tb" already contains a real tab character between a and b, which is why len() counts each escape as a single character.

EscapeMeaning
\nnew line
\ttab
\\a single backslash
\'single quote
\"double quote
\rcarriage return
\u20B9Unicode character by code (₹)
\N{INDIAN RUPEE SIGN}Unicode character by name

Each escape sequence counts as one character: len("a\nb") is 3.

print shows the effect of escapes; repr() and the interactive shell show the escape codes themselves, which is helpful for spotting hidden characters.

An unknown escape such as \d is left as a backslash and a letter, but Python warns about it (SyntaxWarning: invalid escape sequence). Use raw strings for such text.

Raw strings (r"...") switch off escape processing, so r"C:\new" keeps the backslash and n.

Inside triple-quoted strings you can usually type real line breaks instead of \n.

Syntax

Python
print("Line 1\nLine 2")
print("Name:\tAsha")
print("She said \"hi\"")
print("Path: C:\\data")
  • A backslash starts an escape.
  • \\ produces one backslash.
  • Escaped quotes don’t end the string.

Code examples

Common escapes

Python
print("Line 1\nLine 2")
print("Name:\tAsha")
print("She said \"hi\"")
print("Back\\slash")
print(len("a\nb"))
Output
Line 1
Line 2
Name:	Asha
She said "hi"
Back\slash
3

Seeing hidden characters

Python
text = "total\t42\n"
print(repr(text))
print("Price: \u20B9499")
print("\N{INDIAN RUPEE SIGN}")
Output
'total\t42\n'
Price: ₹499
₹

More examples

Tab-separated output and paths

Python
rows = [("Asha", 82, "Pune"), ("Ben", 67, "Delhi")]
print("Name\tMarks\tCity")
for name, marks, city in rows:
print(f"{name}\t{marks}\t{city}")
broken = "C:\new\table.txt"
print(repr(broken))
fixed = r"C:\new\table.txt"
print(fixed, len(fixed))
Output
Name	Marks	City
Asha	82	Pune
Ben	67	Delhi
'C:\new\table.txt'
C:\new\table.txt 16

Step-by-step explanation

  • \t separates columns with tab characters, a format spreadsheets can import.
  • In broken, \n became a newline and \t a tab; repr() reveals those escape codes.
  • The raw string keeps every backslash, so fixed is the real path of 16 characters.
  • When printed, the raw path shows exactly as typed.

Expected output

Running “Tab-separated output and paths” prints exactly:

TextOutput
Name Marks City
Asha 82 Pune
Ben 67 Delhi
'C:\new\table.txt'
C:\new\table.txt 16

Practical applications

  • Exporters write tab-separated files with \t.
  • Log formats separate fields with tabs or use \n between entries.
  • Windows paths and regular expressions are written as raw strings to avoid escape mistakes.

Best practices

  • Use raw strings or forward slashes for Windows paths.
  • Use repr() to inspect strings with hidden characters.
  • Prefer triple-quoted strings for long multi-line text.

Common mistakes

Unintended escapes in paths

Avoid

open("C:\temp\new.txt")

Prefer

open(r"C:\temp\new.txt") # or use forward slashes

\t and \n are interpreted as tab and newline.

Ending a string with a single backslash

Avoid

path = r"C:\folder\"

Prefer

path = "C:\\folder\\"

Even a raw string can’t end with an odd backslash, because it escapes the closing quote.

Key takeaways

  • Escapes start with a backslash: \n, \t, \\, \", \'.
  • repr() shows escape codes; print shows their effect.
  • Raw strings turn escape processing off.

Try it yourself

  1. 1
    Print a two-column table of three fruits and prices separated by tabs.
  2. 2
    Print the text: Use \n for a new line (showing the backslash and n literally).

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic