EMZETT.
Login

Special Characters (Escape Sequences)

In short: A combination of characters that represents a special character that couldn’t otherwise be written directly into a string — e.g. \n for a line break or \" for a quote mark within a quoted string.

In more detail: The backslash character (\) introduces the escape sequence and signals: “the next character isn’t normal text, but has a special meaning”. Without an escape sequence, a quote mark within a string delimited by quote marks, for example, couldn’t be written unambiguously at all, because the string would otherwise end prematurely there.

In Depth

\n   - line break (newline)
\t   - tab
\\   - a single backslash (the backslash itself has to be escaped)
\"   - a quote mark within a double-quoted string
\'   - an apostrophe within a single-quoted string
\r   - carriage return (relevant for Windows line endings)
\0   - null byte (string end in some languages like C)

A concrete example of why this is necessary:

text = "She said: "Hello""  # ERROR - the string already ends at the second quote mark
text = "She said: \"Hello\""  # CORRECT - the inner quote marks are escaped

The compiler/interpreter reads a string character by character and basically knows: “a quote mark ends the string.” Without an escape sequence, there’d be no way to tell the parser “this quote mark is part of the text content, not the end of the string” — the backslash in front of it makes exactly this distinction.

A common pitfall is \\ — because the backslash itself introduces the escape sequence, an actual backslash in the text content (e.g. in a Windows file path like C:\Users\Documents) has to be written doubled. This is exactly a typical source of error when reading paths from configuration files: "C:\Users" is wrongly interpreted by the parser as \U (unknown escape sequence) instead of a literal backslash followed by “Users”. Many languages therefore offer “raw strings” (e.g. Python’s r"C:\Users"), in which escape sequences are deliberately NOT interpreted.

Beyond escape sequences, there are also Unicode escapes for characters that can’t be typed directly on the keyboard, e.g. \u00e9 for “é” — useful when source files are meant to be stored with only ASCII characters, but the string needs to contain international characters.

See also: Strings, String Concatenation