DeprecationWarning invalid escape sequence - what to use instead of d?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- Detect and exclude outliers in a pandas DataFrame
- Detecting 'unusual behavior' using machine learning with CouchDB and Python?
- Determine function name from within that function
- Determine if 2 lists have the same elements, regardless of order?
- Determine if variable is defined in Python
- Determine the type of an object?
- Determining if A Value is in a Set in TensorFlow
- ''dict'' object has no attribute ''has_key''
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.