What are Escape Characters?
Updated Apr 28, 2026
Short answer
Escape characters are special characters used in strings to represent characters that are difficult or impossible to type directly. They are usually written using a backslash (\) followed by another character, such as \n for a new line or \" for a double quote. Escape characters allow programmers to include special formatting, symbols, and control characters inside strings.
Deep explanation
In programming, strings are sequences of characters used to represent text. Some characters have special meanings in programming languages and cannot always be written directly inside a string.
An escape character tells the programming language to interpret the following character differently from its normal meaning. In most languages, the backslash (\) is used as the escape character.
For example, a double quote normally marks the beginning or end of a string:
message = "Hello"If we want to include a quote inside the string, we need to escape it:
message = "She said \"Hello\""The \" tells the language that the quote is part of the text rather than the end of the string.
Common Escape Characters
| Escape Sequence | Meaning | Example |
|---|---|---|
\n | New line | "Hello\nWorld" |
\t | Tab space | "Name:\tJohn" |
\\ | Backslash character | "C:\\Users" |
\" | Double quote | "He said \"Hi\"" |
\' | Single quote | 'It\'s great' |
\r | Carriage return | Moves cursor to line beginning |
\b | Backspace | Removes previous character in some contexts |
How Escape Characters Work
When a compiler or interpreter reads a string, it checks for the escape character.
For example:
text = "Hello\nWorld"The characters stored in the string are not:
Hello\nWorldInstead, \n is interpreted as a newline:
HelloWorldThe escape sequence represents one special character in the final string value.
Escaping Backslashes
Because the backslash itself has a special meaning, it must also be escaped when you want to store an actual backslash.
For example, a Windows file path:
path = "C:\Users\John"can cause problems because sequences like \U may be interpreted as escape sequences.
The correct version is:
path = "C:\\Users\\John"The double backslashes represent actual backslash characters:
C:\Users\JohnEscape Characters vs Raw Strings
Some programming languages provide raw string syntax to avoid escaping backslashes.
In Python:
path = r"C:\Users\John"The r prefix creates a raw string, meaning escape sequences are treated as normal characters.
Without a raw string:
text = "Hello\nWorld"produces:
HelloWorldWith a raw string:
text = r"Hello\nWorld"the output is:
Hello\nWorldWhy Escape Characters Matter
Escape characters are important because they allow developers to:
- Format text with line breaks and indentation.
- Include quotes inside strings.
- Store file paths and special symbols.
- Represent non-printable characters.
- Create properly formatted output.
They are commonly used when working with:
- JSON data.
- File paths.
- Regular expressions.
- SQL queries.
- Command-line arguments.
Real-world example
Suppose an application generates a JSON message containing a user's name:
name = "Alice"message = "{\"user\": \"" + name + "\"}"
print(message)Output:
{"user": "Alice"}The double quotes inside the JSON structure need to be escaped because the programming language also uses double quotes to define the string.
Another example is creating formatted output:
print("Name:\tAlice\nAge:\t25")Output:
Name: AliceAge: 25Here:
\tcreates spacing between labels and values.\nmoves the output to the next line.
Common mistakes
- * Forgetting to escape quotes when placing them inside a string.
- * Using a single backslash in file paths where the language interprets it as an escape sequence.
- * Assuming escape sequences are stored exactly as written instead of being converted to special characters.
- * Confusing the escape character (`\`) with the escaped character (`n` in `\n`).
- * Adding unnecessary escape characters where the language syntax does not require them.
- * Forgetting that different programming languages may support different escape sequences.
Follow-up questions
- Why do we need to escape quotation marks inside strings?
- What does \n represent inside a string?
- Why do backslashes need to be escaped in file paths?
- What is the difference between a normal string and a raw string?
- Are escape characters the same in every programming language?