Python: fix SyntaxWarning: invalid octal escape sequence

Take this code:
path = "C:\477_data"
If we run this with Python 3.11+ in development mode (to enable deprecation warnings), we will see:
$ python3.11 -X dev example.py
/.../example.py:1: DeprecationWarning: invalid octal escape sequence '\477'
path = "C:\477_data"
On Python 3.12+, we instead get a SyntaxWarning which happens at compile time, even without development mode. We can see this by compiling the file with py_compile rather than running it:
$ python3.12 -m py_compile example.py
example.py:1: SyntaxWarning: invalid octal escape sequence '\477'
path = "C:\477_data"
So, what does this warning mean?
Python strings support octal escape sequences: a backslash followed by one to three octal digits (0–7), such as \120, which represents the character with that codepoint. Three octal digits can encode any value from 0 to 0o777 (511 in decimal), but octal escapes were only ever intended to cover single bytes, 0 to 0o377 (255 in decimal), the range used by earlier character sets like Latin-1.
\477 is one such over-large escape: 0o477 is 319 in decimal, above that 0o377 limit. In a str, Python doesn’t clamp or wrap the value, it uses it as a full Unicode codepoint, however large:
>>> ord("\477")
319
>>> "\477"
'Ŀ'
That Ŀ is U+013F LATIN CAPITAL LETTER L WITH MIDDLE DOT, an obscure character. It’s probably not what the developer intended, which is why Python introduced this warning. Curiously, octal escape sequences in bytes literals behave differently, since a byte can only hold values 0–255: Python masks the value down to fit, so b"C:\477_data" becomes b"C:?_data" (319 & 0xFF is 63, the code point for ?), an equally surprising, silent, result.
This warning was added in Python 3.11. From the release notes:
Octal escapes in string and bytes literals with values larger than0o377(255 in decimal) now produce aDeprecationWarning. In a future Python version, they will raise aSyntaxWarningand eventually aSyntaxError. (Contributed by Serhiy Storchaka in gh-81548.)
And, as promised, Python 3.12 upgraded it, from the 3.12 release notes:
Octal escapes with value larger than0o377(ex:"\477"), deprecated in Python 3.11, now produce aSyntaxWarning, instead ofDeprecationWarning. In a future Python version they will be eventually aSyntaxError. (Contributed by Victor Stinner in gh-98401.)
This is the same version-by-version escalation that “regular” invalid escape sequences went through, as covered in a previous post, one Python version later. The eventual promotion to SyntaxError hasn’t happened yet, even in Python 3.15 (due October 2026).
To fix this warning, try to understand the author’s intention when writing it. In most cases, the backslash wasn’t intended to start an octal escape sequence, for example when writing out a Windows path or a regular expression, so you can double the backslash to make it a literal backslash:
path = "C:\\477_data"
In rare cases, if a genuine octal escape was meant, you can substitute the rendered character.
Check out my new book Boost Your GitHub DX.
One summary email a week, no spam, I pinky promise.
Related posts:
- Why does Python log a DeprecationWarning saying “invalid escape sequence”?
- Why does Python log a warning for “invalid decimal literal”?
- Python: fix
SyntaxWarning: "is" with a literal
Tags: python