juniorStrings

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:

Python
message = "Hello"

If we want to include a quote inside the string, we need to escape it:

Python
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 SequenceMeaningExample
\nNew line"Hello\nWorld"
\tTab space"Name:\tJohn"
\\Backslash character"C:\\Users"
\"Double quote"He said \"Hi\""
\'Single quote'It\'s great'
\rCarriage returnMoves cursor to line beginning
\bBackspaceRemoves 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:

Python
text = "Hello\nWorld"

The characters stored in the string are not:

TEXT
Hello\nWorld

Instead, \n is interpreted as a newline:

TEXT
Hello
World

The 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:

Python
path = "C:\Users\John"

can cause problems because sequences like \U may be interpreted as escape sequences.

The correct version is:

Python
path = "C:\\Users\\John"

The double backslashes represent actual backslash characters:

TEXT
C:\Users\John

Escape Characters vs Raw Strings

Some programming languages provide raw string syntax to avoid escaping backslashes.

In Python:

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:

Python
text = "Hello\nWorld"

produces:

TEXT
Hello
World

With a raw string:

Python
text = r"Hello\nWorld"

the output is:

TEXT
Hello\nWorld

Why 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:

Python
name = "Alice"
message = "{\"user\": \"" + name + "\"}"
print(message)

Output:

JSON
{"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:

Python
print("Name:\tAlice\nAge:\t25")

Output:

TEXT
Name: Alice
Age: 25

Here:

  • \t creates spacing between labels and values.
  • \n moves 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?

More Strings interview questions

View all →