Why is ‘\x’ invalid in Python?

There is a table listing all the escape codes and their meanings in the documentation.

Escape Sequence    Meaning                        Notes
\xhh               Character with hex value hh    (4,5)

Notes:

4. Unlike in Standard C, exactly two hex digits are required.
5. In a string literal, hexadecimal and octal escapes denote the byte
with the given value; it is not necessary that the byte encodes a character
in the source character set. In a Unicode literal, these escapes denote a
Unicode character with the given value.

Leave a Comment