Python Strings : Escape Characters
In Python, escape characters are used to represent special characters within strings that are not easy or possible to type directly. They allow you to include things like newlines, tabs, quotes, and other special characters in strings. Escape characters start with a backslash (\), followed by the character to escape.
Here are the most commonly used escape characters in Python:
Common Escape Characters
- Newline (
\n) Inserts a new line into the string.text = "Hello\nWorld" print(text) # Output: # Hello # World - Tab (
\t) Inserts a horizontal tab.text = "Hello\tWorld" print(text) # Output: "Hello World" - Backslash (
\\) Inserts a backslash character (\).text = "This is a backslash: \\" print(text) # Output: "This is a backslash: \" - Single Quote (
\') Inserts a single quote in a string that is enclosed in single quotes.text = 'It\'s a beautiful day' print(text) # Output: "It's a beautiful day" - Double Quote (
\") Inserts a double quote in a string that is enclosed in double quotes.text = "He said, \"Hello!\"" print(text) # Output: 'He said, "Hello!"' - Carriage Return (
\r) Moves the cursor to the beginning of the line without advancing to the next line. If there are characters after\r, they will overwrite the characters at the start of the line.text = "Hello\rWorld" print(text) # Output: "World" # "World" overwrites "Hello" - Backspace (
\b) Inserts a backspace, effectively removing the character before it.text = "Helloo\b World" print(text) # Output: "Hello World" - Form Feed (
\f) Moves the text to the next page or line in printed documents. It is rarely used outside of specific printing contexts.text = "Hello\fWorld" print(text) # Output: "Hello□World" (Depends on environment; shows a form feed effect) - Octal and Hex Values You can use octal (
\ooo) or hexadecimal (\xhh) values to represent characters.text = "\101" # Octal for 'A' print(text) # Output: "A" text = "\x41" # Hexadecimal for 'A' print(text) # Output: "A"
Raw Strings with r Prefix
If you want to prevent escape sequences from being processed, you can use raw strings by prefixing the string with an r or R. This is especially useful for file paths or regular expressions where backslashes are common.
raw_string = r"C:\Users\John\Documents"
print(raw_string)
# Output: "C:\Users\John\Documents" (Backslashes are not treated as escape characters)
Without r prefix:
normal_string = "C:\\Users\\John\\Documents"
print(normal_string)
# Output: "C:\Users\John\Documents"
Unicode Characters
You can use Unicode escape sequences to include characters using their Unicode code points.
- Unicode (4-digit hexadecimal):
\ufollowed by 4 hex digits. - Unicode (8-digit hexadecimal):
\Ufollowed by 8 hex digits. - Named Unicode:
\N{name}.
# Unicode escape for "A" and a heart symbol
text = "\u0041" # Unicode for 'A'
print(text) # Output: "A"
text = "\u2665" # Unicode for '♥' (heart)
print(text) # Output: "♥"
# Named Unicode
text = "\N{GREEK CAPITAL LETTER DELTA}"
print(text) # Output: "Δ"
Summary of Escape Characters
\n: Newline\t: Tab\\: Backslash\': Single quote\": Double quote\r: Carriage return\b: Backspace\f: Form feed\ooo: Octal value\xhh: Hex value\uXXXX: Unicode (4-digit)\UXXXXXXXX: Unicode (8-digit)\N{name}: Named Unicode character