Why is '\x' invalid in Python?

Posted by Paul McGuire on Stack Overflow See other posts from Stack Overflow or by Paul McGuire
Published on 2010-04-24T13:43:39Z Indexed on 2010/04/24 13:53 UTC
Read the original article Hit count: 190

I was experimenting with '\' characters, using '\a\b\c...' just to enumerate for myself which characters Python interprets as control characters, and to what. Here's what I found:

\a - BELL
\b - BACKSPACE
\f - FORMFEED
\n - LINEFEED
\r - RETURN
\t - TAB
\v - VERTICAL TAB

Most of the other characters I tried, '\g', '\s', etc. just evaluate to the 2-character string of a backslash and the given character. I understand this is intentional, and makes sense to me.

But '\x' is a problem. When my script reaches this source line:

val = "\x"

I get:

ValueError: invalid \x escape

What is so special about '\x'? Why is it treated differently from the other non-escaped characters?

© Stack Overflow or respective owner

Related posts about python

Related posts about string