Python Glossary Русский

all Python 3Strings (str)term

Raw strings r"..."

creation

Raw strings (r"...") do not process escape sequences: a backslash is an ordinary character. Indispensable for regular expressions and Windows paths.

Syntax

r"строка"  r'строка'

The r prefix changes only how the literal is parsed, not what later code does with the text: r"\d" is just backslash plus d, and the re module still reads it as the digit pattern — you simply avoid doubling backslashes. The classic trap is that a raw string cannot end in an odd number of backslashes: r"C:\" is a SyntaxError, since the backslash still escapes the closing quote even though it stays in the string. For Windows paths, pathlib beats hand-built string literals.

Examples

print(r"\n\t")           # → \n\t  (без переноса и табуляции)
print(len(r"\n"))        # → 2
print(r"C:\Users\name")  # → C:\Users\name
import re
m = re.search(r"\d+", "abc123")
print(m.group())        # → 123
print(r"abc" == "abc")  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy