Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In a normal Python string literal, a backslash starts an escape sequence: n becomes a line break, t becomes a tab, and \ becomes one literal backslash. Use a raw string when you want backslashes preserved, and use a b prefix when you need bytes rather than text. The exact rules matter most for Windows paths, Unicode characters, and warnings about unrecognized escapes.

How Python escape sequences work

Python interprets escapes while it parses a string literal in your source code. The resulting value contains the character or characters represented by that notation. For example, n in an ordinary string becomes a line-feed character; it is not the two-character sequence backslash plus n.

To include a backslash itself in a non-raw string literal, double it. This is the rule stated in the Python 3.14.8 language reference. The source spelling and the value are different: 'C:\Program Files' evaluates to text containing one backslash between C: and Program Files.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose quote marks that reduce extra escaping. In a single-quoted string, use ' to include a single quote; in a double-quoted string, use " to include a double quote.

Common escape sequences

These escapes are recognized in ordinary string literals. The short control-character escapes are often the clearest choices for formatting text.

Escape Result Typical use
\ One backslash Paths or text that must contain a backslash
', " Single or double quote A quote matching the literal’s delimiters
n Line feed Start a new line
t Horizontal tab Tab-separated or indented text
r Carriage return Return to the start of a line in contexts that use it
b Backspace Control-character data
a, f, v Bell, form feed, vertical tab Specialized control-character data
ooo Character represented by up to three octal digits Numeric character notation
xhh Character represented by exactly two hexadecimal digits Numeric character notation
uXXXX Unicode code point with exactly four hexadecimal digits Unicode text
UXXXXXXXX Unicode code point with exactly eight hexadecimal digits Unicode text
N{name} Unicode character identified by its Unicode name Readable notation for a named character

A backslash followed by a physical newline in an ordinary string literal is ignored; it does not insert a newline into the value. For multiline text, triple-quoted strings are usually clearer. This rule applies inside a string literal and is distinct from using a backslash to continue a Python statement outside a string.

Put a newline or tab in a string

Use n for a line feed and t for a horizontal tab. To retain a visible backslash followed by the letter n, escape the backslash as well.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
message = 'First linenSecond line'
print(message)

label = 'Name:tAda'
print(label)

literal = 'The characters \n are not a line break.'
print(literal)

The first value contains a line break; the last contains a backslash and an n. A useful distinction is that n in source becomes a newline, while \n becomes the two characters and n.

Write Windows paths without accidental escapes

A Windows path written as 'C:Usersname' is unsafe: Python sees U as the start of an eight-digit Unicode escape, so this spelling can raise a Unicode escape syntax error. Use one of these forms instead:

  • path = 'C:\Users\name' doubles each backslash in an ordinary string.
  • path = r'C:Usersname' preserves the backslashes in a raw string, provided the literal does not end with a single backslash.

For paths assembled from parts or where a trailing separator matters, use a path-handling API rather than trying to force a raw literal to represent an impossible ending. Raw strings cannot end with an odd number of backslashes because the final backslash would escape the closing quote.

When to use a raw string

Prefix a string literal with r or R when backslashes should remain backslashes in its value. This is convenient for regular-expression patterns and many paths:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
pattern = r'd{4}-d{2}-d{2}'

A raw string is still an ordinary str; rawness changes how the source literal is parsed, not the runtime type. It also does not remove quote rules. You may escape a quote in a raw literal, but that backslash remains part of the value. If the text must end in a backslash, use a different representation, such as a normal string with a doubled backslash or a path API.

Unicode escapes in text

Use u followed by exactly four hexadecimal digits or U followed by exactly eight to write a Unicode code point. The named form N{name} can be easier to read when you know the Unicode character name. These are source-code notations for characters in a text string; they are not a way to decode data read later from a file, network connection, or user input.

When converting text into bytes, choose an explicit encoding at that conversion boundary. Writing a Unicode escape in a string literal does not itself encode the text as bytes.

String literals and bytes literals are different

A b or B prefix creates a bytes object, not a str. Bytes literal source content is ASCII; values above 127 and a zero byte must be represented with escapes, commonly hexadecimal or octal notation. The Unicode escapes u, U, and N are not supported in bytes literals.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Need Use Important distinction
Human-readable text A string literal such as 'Hellon' Unicode escapes represent characters in a str.
Raw byte values A bytes literal such as b'x41x00' Hex and octal escapes represent byte values; Unicode escapes are unavailable.
Text encoded for storage or transmission Encode a str with an explicit encoding Encoding is a conversion step, not the same operation as parsing a literal escape.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What an invalid escape warning means

The Python language reference says an unrecognized escape is left unchanged, including its backslash. For example, 'q' retains a backslash followed by q. However, relying on this behavior is poor practice: unrecognized escapes have produced a DeprecationWarning since Python 3.6, became a SyntaxWarning in Python 3.12, and are intended to become a SyntaxError in a future release.

Fix the literal rather than silencing the warning. Double the backslash if it must be part of an ordinary string, or use a valid raw literal when preserving backslashes is the intent. Check the Python version targeted by your code when interpreting which warning category appears.

Octal escapes have a related version caveat: values above 0o377 began producing a DeprecationWarning in Python 3.11 and a SyntaxWarning in Python 3.12; the reference says they are intended to become errors in a future version. Keep octal escapes within that range, and prefer a clearer Unicode notation when the intended value is Unicode text.

Escapes in f-strings

In an f-string, literal text outside replacement fields follows ordinary string rules: escapes are interpreted unless the f-string is also raw. The expression inside a replacement field is evaluated. Python 3.12 removed several restrictions on expressions in f-strings, including the earlier restriction on backslashes inside replacement expressions; only rely on that syntax when Python 3.12 or later is your target.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A raw f-string, such as rf'...', preserves backslashes in its literal portions. It does not make replacement expressions raw or stop them from being evaluated.

Choose the right representation

  • Need text with formatting? Use a normal string and recognized escapes such as n or t.
  • Need backslashes preserved? Use a raw string when its quote and ending-backslash constraints fit; otherwise double the backslashes.
  • Need bytes rather than characters? Use a bytes literal for byte values, or encode text explicitly when converting a string.
  • Need a Unicode character? Use the literal character, a correctly sized u or U escape, or its N{name} form.
  • Seeing an invalid-escape warning? Correct the source spelling and account for the Python version that runs the code.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.