← Back to Chapters

Python String Escape Characters

? Python String Escape Characters

⚡ Quick Overview

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.

? Key Concepts

  • An escape character starts with a backslash \ and gives special meaning to the next character.
  • Useful for new lines, tabs, quotes inside strings, and more.
  • Raw strings (like r"...") treat backslashes as normal characters.
  • You can combine multiple escape characters in a single string.

? Common Escape Characters

  • \n – New line
  • \t – Horizontal tab
  • \\ – Backslash
  • \' – Single quote
  • \" – Double quote

? Syntax & Theory

In Python, strings are usually written inside single ('...') or double ("...") quotes. Escape sequences are written inside these strings:

  • General form: "Some text\nMore text"
  • Backslash makes magic: \n is interpreted as a newline, \t as a tab.
  • Escaping quotes: use \' inside single-quoted strings, \" inside double-quoted strings.
  • Raw strings: prefix the string with r to stop Python from treating \ as an escape starter.

? Basic Escape Characters in Action

? View Basic Escape Examples
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

Raw strings treat backslashes as literal characters. They are especially useful for Windows file paths and regular expressions, where backslashes appear frequently.

? View Raw String Examples
path = r"C:\Users\Alice\Documents"
print(path)  # C:\Users\Alice\Documents

regex = r"\d+\s\w+"
print(regex) # \d+\s\w+

? Combining Escape Characters

You can combine multiple escape characters to format multi-line output neatly:

? View Combined Escape Example
print("Name:\tAlice\nAge:\t25\nCity:\tNew York")

?️ Live Output & Explanation

? What the Output Looks Like

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.

? When to Use Escape Characters

  • Formatting console output into multiple lines and columns.
  • Including quotes inside strings without ending the string.
  • Representing file paths or patterns that contain backslashes.
  • Writing regular expressions where backslash sequences are common.

? Tips & Best Practices

  • Use raw strings (r"...") for Windows file paths to avoid having to double every backslash.
  • Use \n and \t to make console output clean and readable.
  • Escape quotes if they match the string delimiters:
    • 'It\'s Python'
    • "She said, \"Hi\""
  • Avoid using plain backslashes in normal strings like "C:\Users\Alice" – they may be treated as escape sequences.
  • Be careful with raw strings ending with a single backslash (e.g. r"C:\Users\") – that is invalid in Python.

? Try It Yourself

  • Print a multi-line address using \n, for example:
    Name, Street, City, Country each on a new line.
  • Create a tabulated output of items and prices using \t, like:
    Item Price, each on separate lines with tabs.
  • Use a raw string for a Windows file path (e.g. r"C:\Program Files\Python") and print it.
  • Print a sentence that includes both single and double quotes, for example:
    It's called "Python".