Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
807 views
in Technique[技术] by (71.8m points)

string - Why is 'x' invalid in Python?

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

a - BELL
 - BACKSPACE
f - FORMFEED

 - LINEFEED

 - RETURN
 - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...