60

I'm getting lots of warnings like this in Python:

DeprecationWarning: invalid escape sequence \A
  orcid_regex = '\A[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]\Z'

DeprecationWarning: invalid escape sequence \/
  AUTH_TOKEN_PATH_PATTERN = '^\/api\/groups'

DeprecationWarning: invalid escape sequence \
  """

DeprecationWarning: invalid escape sequence \.
  DOI_PATTERN = re.compile('(https?://(dx\.)?doi\.org/)?10\.[0-9]{4,}[.0-9]*/.*')

<unknown>:20: DeprecationWarning: invalid escape sequence \(

<unknown>:21: DeprecationWarning: invalid escape sequence \(

What do they mean? And how can I fix them?

Sean Hammond
  • 12,550
  • 5
  • 27
  • 35
  • 3
    With Python 3.8-dev this will give you a `SyntaxWarning` instead of `DeprecationWarning` and the same solution applies. – RajaRaviVarma Feb 15 '19 at 10:30

2 Answers2

96

\ is the escape character in Python string literals.

For example if you want to put a tab character in a string you would do:

>>> print("foo \t bar")
foo      bar

If you want to put a literal \ in a string you have to use \\:

>>> print("foo \\ bar")
foo \ bar

Or use a "raw string":

>>> print(r"foo \ bar")
foo \ bar

You can't just go putting backslashes in string literals whenever you want one. A backslash isn't valid when not followed by one of the valid escape sequences, and newer versions of Python print a deprecation warning. For example \A isn't an escape sequence:

$ python3.6 -Wd -c '"\A"'
<string>:1: DeprecationWarning: invalid escape sequence \A

If your backslash sequence does accidentally match one of Python's escape sequences, but you didn't mean it to, that's even worse.

So you should always use raw strings or \\.

It's important to remember that a string literal is still a string literal even if that string is intended to be used as a regular expression. Python's regular expression syntax supports lots of special sequences that begin with \. For example \A matches the start of a string. But \A is not valid in a Python string literal! This is invalid:

my_regex = "\Afoo"

Instead you should do this:

my_regex = r"\Afoo"

Docstrings are another one to remember: docstrings are string literals too, and invalid \ sequences are invalid in docstrings too! Use raw strings (r"""...""") for docstrings if they contain \'s.

Sean Hammond
  • 12,550
  • 5
  • 27
  • 35
  • 2
    If you have a variable that contains the string, though, how do you convert it to a raw string so it isn't misinterpreted? This isn't working for me: `r"{}".format(my_variable)` – HaPsantran Apr 10 '19 at 00:28
  • A raw string is not a different type to a regular string, its is just a different way of writing a string literal in your source code. Once you have a variable containing the string, is a string. There is no difference or distinction between regular or raw – AbrahamCoding Dec 02 '19 at 05:26
  • 2
    Escaping the backslash applies to regular expressions where you are escaping the dot. This is where it came up for me. See [my answer here to "Regular expression to match a dot"](https://stackoverflow.com/questions/13989640/regular-expression-to-match-a-dot/66666859#66666859). So, you must do `"\\."` or `r"\."` instead of `"\."`. Took me a while to figure out. Your answer helped. Thanks. – Gabriel Staples Mar 20 '21 at 21:11
-1

When I changed \ to \\ in the name of the path, Resolved the Invalid escape sequence warning. For your reference please find the attached screenshot below. I was getting this error in pycharm:

enter image description here

enter image description here

smci
  • 32,567
  • 20
  • 113
  • 146
Arpan Saini
  • 4,623
  • 1
  • 42
  • 50