Skip to content

Escape Characters Cheat Sheet

Escape characters are special sequences used inside strings to represent characters that are otherwise hard or impossible to write directly, such as newlines, tabs, quotes, or control characters.

They are essential in programming languages, regular expressions, JSON, shells, and configuration files.


What Is an Escape Character?

An escape character changes the interpretation of the next character.

Most languages use the backslash (\) as the escape character.


Common Escape Sequences

Escape Meaning Description
\n Newline Line break
\t Tab Horizontal tab
\r Carriage Return Move to line start
\b Backspace Remove previous char
\f Form Feed Page break
\\ Backslash Literal \
\" Double Quote Inside double-quoted strings
\' Single Quote Inside single-quoted strings
\0 Null Null character

String Examples

Hello\nWorld
Hello\tWorld

Escaping Quotes

printf("She said: \"Hello\"");
'It\'s working'

Python Raw Strings

r"C:\Users\Admin"

Unicode Escapes

"\u0041"  // A
char c = '\u0041';

JSON Escaping

{
  "path": "C:\\Program Files\\App"
}

Regex Escaping

\.
"\\."

Shell Escaping

echo -e "Hello\nWorld"

Common Pitfalls

  • Double escaping
  • Unescaped backslashes in paths
  • Regex inside strings
  • JSON parse errors

Summary

  • Escape characters change string interpretation
  • Backslash is the primary escape character
  • Context matters (string, regex, JSON, shell)