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:
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.
This prints:
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.
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.
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:
If you need a trailing backslash, use a normal string with escaping:
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:
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.

