Escape sequences in Python strings are backslash codes such as \n (newline), \t (tab) and \\ (a literal backslash) that stand for characters you cannot type directly or that would otherwise end the string. Python escape characters are processed when the source code is compiled, so the string object holds the real character, not the two-character code. Raw strings, written with an r prefix, turn this processing off.
The examples target Python 3.13 and 3.14. You will learn the full list of escape sequences, how to include quotes and Unicode characters, how raw strings work, and why invalid escapes such as "\d" now produce a warning.
A backslash inside a normal string literal tells Python that the next characters have a special meaning.
| Sequence | Meaning |
|---|---|
\n | Newline (line feed) |
\t | Horizontal tab |
\r | Carriage return |
\\ | A single backslash |
\' | Single quote |
\" | Double quote |
\a | Bell (alert) |
\b | Backspace |
\f | Form feed |
\v | Vertical tab |
\0 to \377 | Octal value, up to three digits |
\xhh | Character with two-digit hex value |
\uXXXX | Unicode character with four hex digits |
\UXXXXXXXX | Unicode character with eight hex digits |
\N{name} | Unicode character by its official name |
A backslash at the very end of a line inside a string literal removes the line break, which lets you split a long literal across lines.
The first line uses \t and \n, so the output spans two lines with tab spacing. The \\ sequence is needed because a single backslash would start another escape.
You can avoid most quote escapes by choosing the other kind of quote for the delimiter. Escapes are only needed when a string contains both kinds.
Triple-quoted strings can also span several lines, and \n is still processed inside them.
Python 3 strings are Unicode text, so you can name any character by code point or by name.
\x takes exactly two hex digits and \u exactly four; fewer digits is a SyntaxError. In bytes literals only \x, octal and the simple escapes work: \u and \N are not special in b"...".
A raw string, written r"...", keeps backslashes as they are. This is the usual choice for regular expressions and Windows paths.
Raw strings have one limitation: they cannot end with an odd number of backslashes, because the last backslash would escape the closing quote. r"C:\folder\" is a syntax error. Write "C:\\folder\\" or use pathlib.Path and avoid hand-written separators:
Raw strings also work as rb"..." for bytes and rf"..." for raw f-strings, where {} placeholders are still evaluated but backslashes are not processed.
If you write a backslash followed by a character that is not a valid escape, such as "\d", Python keeps both characters, but warns. Before Python 3.12 this was a DeprecationWarning, which was hidden by default. From Python 3.12 it is a SyntaxWarning that is shown, and the Python documentation says it is planned to become a SyntaxError in a future release.
Fix the warning by using a raw string or by doubling the backslash. Do not rely on unknown sequences being preserved.
print() shows the actual characters, while repr() shows an escaped, source-like form. This is helpful when debugging hidden whitespace.
The string has 13 characters: \n and \t each count as one. If you need to write Python source back out, use repr(); for ASCII-only output with escapes for non-ASCII characters, use ascii().
Write "\\" in a normal string. In a raw string a single backslash is fine, except at the end of the literal.
\n is a line feed, used on Linux and macOS. \r\n (carriage return plus line feed) is the Windows line ending. Text-mode files translate line endings for you, so you rarely write \r\n yourself.
Sequences like \n and \t in "C:\new\table" become real characters. Use a raw string, forward slashes, or pathlib.
Yes. In Python 3.12 and later you can also use backslashes inside the braces of an f-string expression; before 3.12 that was not allowed.
Escape sequences begin with a backslash and let a string contain newlines, tabs, quotes, backslashes and any Unicode character. Use \\ for a literal backslash, \uXXXX or \N{name} for Unicode, and raw strings for regular expressions and paths. Avoid invalid sequences like "\d", because Python 3.12 and later warns about them, and use repr() to see exactly what a string contains.