Python
DeprecationWarning
escape sequence
regex
programming

DeprecationWarning invalid escape sequence - what to use instead of d?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This warning appears when Python parses a normal string literal that contains a backslash sequence it does not recognize. The most common example is a regular expression like "\d+", where \d is meaningful to the regex engine but not to the Python string parser.

Why Python Warns About \d

Python processes string escapes before the re module ever sees the pattern. In a normal string literal, Python understands escapes such as \n and \t, but \d is not a valid Python escape. That is why code like this triggers a warning:

python
1import re
2
3pattern = "\d+"
4print(re.findall(pattern, "Room 42 on floor 7"))

The regular expression itself is fine. The warning comes from the Python string literal, not from the regex syntax.

Use a Raw String for Regex Patterns

The usual fix is to make the pattern a raw string. Raw strings tell Python to treat backslashes literally, which is exactly what you want for most regular expressions.

python
1import re
2
3pattern = r"\d+"
4print(re.findall(pattern, "Room 42 on floor 7"))

This prints:

text
['42', '7']

That r prefix is the idiomatic answer in Python because it keeps regex patterns readable. Without it, every backslash in the pattern usually has to be doubled, which becomes painful in longer expressions.

Escaping the Backslash Also Works

If you cannot use a raw string for some reason, escape the backslash so the regex engine still receives \d.

python
1import re
2
3pattern = "\\d+"
4print(re.findall(pattern, "Room 42 on floor 7"))

This works because Python turns \\ into a literal backslash, leaving the regex engine with the intended \d+ pattern.

The raw-string version is usually easier to read:

  • raw string: r"\d+"
  • escaped string: "\\d+"

Both are correct. Raw strings are simply less noisy.

Use Plain d When You Do Not Mean Regex

Sometimes the warning appears because someone copied regex syntax into code that is not using regular expressions at all. If you just want the letter d, remove the backslash.

python
text = "abcde"
print("d" in text)

That is a different operation from regex matching. Use the backslash only when the receiving API expects regex syntax.

Know the Limits of Raw Strings

Raw strings are convenient, but they are not magical. They still follow Python string literal rules, which means a raw string cannot end with a single trailing backslash. For example, this is invalid:

python
# SyntaxError
# bad = r"c:\temp\"

If you need a trailing backslash, use a normal string with escaping:

python
path = "c:\\temp\\"
print(path)

That limitation matters for file paths, though for regex patterns it is rarely a problem.

Keep Regexes Readable and Testable

Once a pattern becomes more complex than \d+, raw strings become even more valuable. Compare the readability of these two equivalent patterns:

python
1import re
2
3raw_pattern = r"^\d{4}-\d{2}-\d{2}$"
4escaped_pattern = "^\\d{4}-\\d{2}-\\d{2}$"
5
6for value in ["2026-03-07", "03/07/2026"]:
7    print(value, bool(re.match(raw_pattern, value)))
8    print(value, bool(re.match(escaped_pattern, value)))

Both versions work, but the raw string is easier to verify visually. That matters when patterns are reviewed or maintained months later.

Common Pitfalls

The biggest mistake is assuming the warning comes from the re module, when it actually comes from Python parsing the string literal first. Another common issue is fixing one \d by doubling the backslash but leaving other regex escapes such as \s or \b untouched in the same file. Developers also mix Windows file paths and regex examples, even though they have different quoting needs. Finally, some code uses a backslash before ordinary letters in non-regex strings, which creates warnings without providing any useful meaning.

Summary

  • The warning is caused by Python string parsing, not by the regex engine.
  • For regex, prefer raw strings such as r"\d+".
  • Escaping the backslash, as in "\\d+", is also correct but less readable.
  • If you do not mean regex, remove the backslash and use the literal character.
  • Raw strings are ideal for most regexes, but they cannot end with a single backslash.

Course illustration
Course illustration

All Rights Reserved.