For AI agents: use /llms.txt for the Nakafa content index.
An escape sequence starts with a backslash (\) followed by one or more characters that Python interprets specially. It lets a string literal represent characters such as a line break or a quote that would otherwise end the literal.
The backslash changes how Python parses the following characters. It is part of the source notation, while the resulting string contains the character represented by that notation.
When the same quote character both delimits a string and appears inside it, an unescaped inner quote closes the literal too early. The remaining characters are then parsed as invalid Python code.
# Problem: single quote inside string
>>> 'I'm Bob.'
SyntaxError: invalid syntax
# Solution: use double quotes for string
>>> "I'm Bob."
"I'm Bob."
# Problem: both types of quotes in string
>>> 'He said: "I'm Bob."'
SyntaxError: invalid syntaxWhen you use single quotes to create a string, but there's also a single quote inside that string, Python will consider the second quote as the string closer. As a result, the part after it is considered invalid code.
Escape sequences provide a solution by using a backslash (\) before the character you want to insert. The backslash tells the computer that the next character should be treated as a regular character, not as a special character.
# Using escape sequence for single quote
>>> 'He said: "I\'m Bob."'
'He said: "I\'m Bob."'
# When printed, escape sequence is interpreted
>>> print('He said: "I\'m Bob."')
He said: "I'm Bob."In this example, \' tells Python that the single quote is part of the text, not the string end marker.
Since backslash has a special meaning in escape sequences, how do you display the backslash itself? You need to use two backslashes consecutively.
# Displaying backslash in string
>>> print('The escape sequence for a new line is \\n.')
The escape sequence for a new line is \n.
# Using raw string (prefix r)
>>> text = r'The escape sequence for a new line is \n.'
>>> text
'The escape sequence for a new line is \\n.'
>>> print(text)
The escape sequence for a new line is \n.A raw string prefix such as r preserves most backslashes instead of interpreting them as escape sequences. Quotes still have to fit the literal's delimiter rules, and a raw string cannot end with an odd number of backslashes.
Here are escape sequences commonly used in programming:
| Escape Sequence | Meaning |
|---|---|
\' | Single quote (') |
\" | Double quote (") |
\n | New line (newline) |
\r | Carriage return |
\t | Horizontal tab |
\b | Backspace |
\\ | Backslash character () |
\uxxxx | Unicode character with a four-digit hexadecimal code point |
\Uxxxxxxxx | Unicode character with an eight-digit hexadecimal code point |
\N{name} | Unicode character by its registered name |
\xhh | Character with a two-digit hexadecimal value |
Each escape sequence has a specific function to control the display or format of text in programs.
In str.format() and f-strings, curly braces delimit replacement fields. Doubling a brace escapes it for the formatting syntax. This is separate from backslash escape sequences in string literals.
# Escaped curly braces in a format string: {{ }}
>>> '{{}}'.format('text')
'{}'
# Displaying text with curly braces
>>> '{{{}}}'.format('text')
'{text}'Write {{ to produce { and }} to produce }. In '{{{}}}', the outer doubled braces become literal braces while the middle {} remains a replacement field.
Let's see how escape sequences are used in real situations to create neat and well-formatted output.
# Creating formatted output with tab and newline
print("Name\t: John Doe")
print("Age\t: 25 years")
print("Address\t: Jakarta\n")
# Displaying dialog with quotation marks
print('He said: "Today the weather is very bright!"')
# Displaying file path with backslash
print("File saved at: C:\\Users\\Documents\\data.txt")
# Combination of various escape sequences
print("First line\nSecond line\t(with tab)\nThird line")In this example, \t creates neat columns, \n separates lines, and \\ displays Windows file paths correctly.
The key distinction is between source notation and the resulting string. \n, for example, occupies two characters in the source but represents one newline character in the string.