String escape characters and sequences

What string escape characters and sequences do, how they differ across C, JavaScript, Python and JSON, and when escaped text stays readable and usable.

What a string escape character is

An escape character tells a parser not to read the next character or characters literally. In most programming languages and data formats the escape character is the backslash. A string escape sequence such as \n or " is a short piece of source text that stands for a different character in the resulting value. That might be a line break, or a quote mark that would otherwise end the string. Some contexts escape differently. Standard SQL, for example, writes a single quote inside a string as two single quotes, as in 'O''Brien'.

What escaping actually changes

Escaping changes how a value is written down, not the value itself. The source text and the stored string are different things. The source contains a backslash and a letter, and the stored string contains the single character they represent. Escaping is not visual styling. A newline produced by \n is a real control character, and a tab produced by \t is a real tab. The transformation controls exactly which characters a program receives, and it keeps invisible or syntax-breaking characters out of the source.

const label = "Tab:\tEnd";
console.log(label.length);   // 8: the two characters \t became one tab

const wrong = "C:\temp\new";   // \t and \n are read as tab and newline
const right = "C:\\temp\\new"; // value is C:\temp\new

The Windows-path line shows the most common escaping mistake: a backslash meant literally gets combined with the letter after it. Many languages let you switch escape processing off. Python has raw strings, such as r"C:\temp", and JavaScript has the String.raw tag. These are usually the cleaner choice for paths and regular expressions. Raw strings have their own limits; in Python, a raw string still cannot end with an odd number of backslashes.

Code points, combining marks and emoji

Unicode escapes name code points, and a code point is not always what a reader sees as one character. You can write é as a single precomposed code point, or as a plain e followed by a combining acute accent. The two strings look the same, but they compare as unequal and report different lengths. Emoji outside the Basic Multilingual Plane count as one code point in Python but as two UTF-16 code units in JavaScript and JSON. A family emoji built with zero-width joiners spans several code points. The examples below show standard documented language behavior. They are illustrations, not measured output from any particular tool.

>>> len("\u00e9"), len("e\u0301")
(1, 2)
>>> "\u00e9" == "e\u0301"
False
>>> len("\U0001F600")
1
>>> len("\U0001F468\u200D\U0001F469\u200D\U0001F467")
5
"\u{1F600}".length                // 2
"\u{1F600}" === "\uD83D\uDE00"    // true

In practice, output can look right and still differ character by character. This happens when a tool escapes or unescapes text by cutting at fixed positions, or when it normalizes Unicode along the way. Compare values in code rather than by eye. If equivalent spellings must match, normalize both sides first, for example with unicodedata.normalize in Python or String.prototype.normalize in JavaScript.

How escape sequences differ between languages and formats

Sequence | Meaning | C | JavaScript | Python | JSON
\n | Line feed | Yes | Yes | Yes | Yes
\t | Tab | Yes | Yes | Yes | Yes
\ | Backslash | Yes | Yes | Yes | Yes
" | Double quote | Yes | Yes | Yes | Yes
' | Single quote | Yes | Yes | Yes | No
\xHH | Character by hex value | Yes, reads all following hex digits | Yes, exactly two digits | Yes, exactly two digits | No
\uXXXX | Four-digit code point | Yes since C99, with restrictions | Yes | Yes in str, not in bytes | Yes
\U00XXXXXX | Eight-digit code point | Yes since C99 | No; use \u{...} | Yes in str | No; use a surrogate pair
\0 | Null character | Yes (octal escape) | Yes, if no digit follows | Yes (octal escape) | No; use \u0000

These differences matter most when text moves between systems.
JSON is the strictest. It accepts only ", \, /, \b, \f, \n, \r, \t and \uXXXX. It rejects single-quote and hex escapes, and it requires control characters to be escaped.
Other languages disagree about escapes they don't recognize. JavaScript drops the backslash, so "\q" becomes "q". Python keeps the backslash and, since version 3.12, issues a SyntaxWarning. The C standard requires a diagnostic.
Java processes \uXXXX escapes before it parses the source. As a result, \u000A inside a string literal causes a compile error instead of producing a newline.

Here is how the same ideas look in JSON. If the emoji is escaped, it must be written as a UTF-16 surrogate pair. JSON also allows the character to appear unescaped.

{"quote": "She said \"hi\"", "path": "C:\\temp", "smile": "\uD83D\uDE00"}

Layered contexts multiply backslashes. A regular expression that matches digits is \d+. Written in an ordinary Python or Java string literal, it becomes \d+, because the string parser removes one level of escaping before the regex engine sees the pattern. Putting that string inside JSON doubles the backslashes again. Each layer must be escaped exactly once, in order. Escaping twice at the same layer leaves visible backslashes in the result, and skipping a layer changes the meaning.

Will escaped text stay readable and usable after copying or sharing?

  • Copying from code to a document: copying from source code or a log that shows escaped output gives you the backslash sequences, not the characters they stand for. Pasting \n into a word processor or chat message produces a backslash and an n, not a line break.
  • Copying from a document to code: pasting text with real line breaks, tabs or quotes into a string literal or JSON field can break the syntax unless you escape it first.
  • Search and editing: a search for O'Brien will not find 'O''Brien' or O'Brien in source. Find-and-replace on escaped text can split a sequence and corrupt it.
  • Assistive technology: escaped text is written for parsers. A screen reader will probably announce backslashes and letters rather than the intended characters. No screen reader was tested for this overview, so check the behavior of a specific product in its documentation or with a direct test.
  • Invisible characters: escaping turns invisible characters such as zero-width joiners or non-breaking spaces into visible sequences like \u200D or \u00A0. That helps with debugging but makes the text harder for people to read.

Use a plain-text version whenever a person, not a parser, is the reader. That includes documentation, user-facing messages, alt text, chat and email. Keep the escaped form inside code, configuration and data files, and unescape it before displaying it to people.

Escaping is not sanitization

Escaping text for one context does not make it safe in another. A JSON-escaped string can still contain HTML that runs in a browser, so HTML output needs HTML-specific encoding. Backslash-escaping input does not reliably protect SQL either; parameterized queries are the standard defense there. Online escape and unescape tools differ in which syntax they target. A tool's appearance also tells you nothing about whether it keeps what you paste. Don't send credentials or private data to one unless its documentation covers data retention. Alternatively, do the conversion locally with the language's own functions, such as JSON.stringify in JavaScript or json.dumps in Python.

Is escaping the right transformation for your use?

Use escape sequences when text has to sit inside a string literal, a JSON value or another delimited format. They are also the right tool when you need to represent characters that are invisible, unprintable or reserved by the syntax. The main benefit is that the value passes through the parser intact while the source stays easy to edit.
The main tradeoff is readability. Escaped text is precise for machines and awkward for people, and it is only correct for the exact syntax it was written for.
Prefer plain text when people will read, search or edit the content directly. When backslashes are common, prefer raw strings or the language's own serializer over escaping by hand.

Limits of this overview

No subject-specific measurements, tool comparisons or test results were available. The behavior described here follows the documented rules of C, JavaScript, Python, JSON and Java. Details vary with language version, compiler settings and database configuration; some SQL databases also accept backslash escapes depending on their mode. Behavior in particular editors, chat apps and screen readers was not tested, so confirm it in the environment where the text will be used.