Python3.12 SyntaxWarning on triplequoted string `\d` must be `\\d`

After updating to Python 3.12, I get warnings about invalid escape sequence on some triple-quotes comments.

Is this a new restriction? I have the habit of documenting code using triple-quoted string, but this has never been a problem prior to Python 3.12.

python3 --version
Python 3.12.0
$ ./
/ SyntaxWarning: invalid escape sequence '\d'

I tried replacing all lines with \d:


with \\d:


The warning disappears.

Suppressing the warning do not seem to work:

import warnings
warnings.filterwarnings('ignore', category=SyntaxWarning)

Any pointers on how to do this correctly? I hope I do not have to escape all Windows paths documented in triplequotes in our code.


  • Using invalid escape sequences in string literals has been deprecated since Python 3.6. Since then, attempting to use an invalid escape sequence has emitted a DeprecationWarning. This often goes unnoticed if you never run Python with warnings enabled. DeprecationWarnings are silenced by default.

    Python 3.12 upgraded the DeprecationWarning to a SyntaxWarning. SyntaxWarnings are emitted by the compiler when the code is parsed, not when it's being run, so they cannot be ignored using a runtime warning filter. Unlike DeprecationWarnings, SyntaxWarnings are displayed by default, which is why you're seeing it now. This increase in visibility was intentional. In a future version of Python, using invalid escape sequence in string literals is planned to eventually become a hard SyntaxError.

    The obvious solution would be to use comments for comments instead of string literals. Unlike string literals, comments aren't required to be syntactically valid. See the discussion in Python comments: # vs. strings for more on why using string literals for comments is not a recommended practice.

    To address this warning in general, you can make the string literal a raw string literal r"...". Raw string literals do not process escape sequences. For example, r"\n" is treated simply as the characters \ and n and not as a newline escape sequence.