Technology  /  Python

🐍 Python 78 guides · updated 2026

From first variable to OOP, generators, and real projects — the language that runs everything from data pipelines to AI agents, taught the practical way.

Python Escape Characters and Raw Strings: When Backslashes Get Complicated

The backslash is Python’s escape character. It tells the interpreter: “the next character doesn’t mean what it usually means.” This is how you embed newlines, tabs, quotes, and other special characters inside a string. Raw strings flip this on its head — the r prefix tells Python to treat backslashes as literal characters, which turns out to be exactly what you need for file paths and regular expressions.


Escape Sequences

Every escape sequence starts with \ followed by a character or digits:

# Whitespace characters
print("Line one\nLine two") # newline
print("Name:\tValue") # tab
print("Back\bspace") # backspace (deletes previous char)
print("Carriage\rReturn") # carriage return
# Quote characters
print('It\'s working') # escaped single quote
print("She said, \"hello\"") # escaped double quote
# The backslash itself
print("C:\\Users\\Alice") # literal backslash
# Null character (rarely needed)
print("hello\0world") # null byte between words

Unicode and hex escapes

You can embed any Unicode character by code point:

print("©") # © (copyright symbol)
print("☃") # ☃ (snowman)
print("\U0001F600") # 😀 (emoji, 8-digit code)
# Hex escape for ASCII values
print("\x41") # A
print("\x48\x65\x6C\x6C\x6F") # Hello

Octal escape

Less common, but supported:

print("\101") # A (octal 101 = decimal 65 = ASCII 'A')

The Full Escape Sequence Reference

SequenceMeaning
\\Literal backslash
\'Single quote
\"Double quote
\nNewline (line feed)
\rCarriage return
\tHorizontal tab
\bBackspace
\fForm feed
\vVertical tab
\0Null character
\N{name}Unicode character by name
\uXXXXUnicode 16-bit code point
\UXXXXXXXXUnicode 32-bit code point
\xXXHex character value

Raw Strings

A raw string treats every backslash as a literal character. Prefix the string with r or R:

# Regular string — backslash escapes are processed
path = "C:\\Users\\Alice\\Documents"
print(path) # C:\Users\Alice\Documents
# Raw string — backslashes are literal
path = r"C:\Users\Alice\Documents"
print(path) # C:\Users\Alice\Documents

Both print the same thing, but the raw string is easier to read and write, especially for deep paths.

Raw strings for regular expressions

This is where raw strings pay the biggest dividends. Regular expressions use backslashes heavily for metacharacters like \d (digit), \w (word character), and \s (whitespace). Without raw strings, you’d need to double every backslash:

import re
# Without raw strings — each \d needs to be \\d
pattern = "\\d{3}-\\d{3}-\\d{4}"
# With raw strings — much cleaner
pattern = r"\d{3}-\d{3}-\d{4}"
phone = "Call us at 555-867-5309"
match = re.search(pattern, phone)
print(match.group()) # 555-867-5309

The rule of thumb: if a string is going to be used as a regex pattern, make it a raw string.


What Raw Strings Cannot Do

Raw strings can’t end with an odd number of backslashes, because \" would be interpreted as an escaped quote character:

r"path\" # SyntaxError — the \" escapes the closing quote
r"path\\" # works — but represents the string path\\ which prints as path\\

Raw strings also don’t process \n or any other escape sequences, which means they’re not suitable for strings where you actually want newlines or tabs:

regular = "Line 1\nLine 2" # contains actual newline
raw = r"Line 1\nLine 2" # contains literal \n — no newline
print(regular)
# Line 1
# Line 2
print(raw)
# Line 1\nLine 2

Practical Examples

Building Windows file paths

import os
# Platform-independent path construction (preferred)
path = os.path.join("C:\\Users", "Alice", "Documents", "report.txt")
# Raw string when you know the platform
path = r"C:\Users\Alice\Documents\report.txt"
# Forward slashes also work on Windows in Python
path = "C:/Users/Alice/Documents/report.txt"

Using os.path.join() or pathlib.Path is the most portable approach and avoids backslash issues entirely.

Multi-line strings with embedded newlines

# Using \n escape sequences
address = "123 Main Street\nSpringfield, IL 62701\nUSA"
# Using triple quotes — newlines come from the actual line breaks
address = """123 Main Street
Springfield, IL 62701
USA"""

Triple-quoted strings are often cleaner for multi-line content where you want the visual structure of the string to match its output.

Byte strings

Byte strings use b prefix and produce bytes objects instead of str. They support the same escape sequences:

data = b"Hello\x00World" # bytes with null byte
print(len(data)) # 11 — counts bytes, not characters
print(data[0]) # 72 — integer value of 'H'

Byte strings are used for network protocols, file I/O, and binary data — situations where you’re working with raw bytes rather than text.