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

Categories

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

the reason: python string assignments accidentally change '' into 'x08' and 'a' into 'x07', why Python did this?

Had two answers and some comments, mentioned another question, but all had not provided REASON, why Python did this changes? such as '/b' is '/x08' is just the result, but why? Cheers.

I try to add this path"F:ig dataPython_codingdiveintopython-5.4py" into sys.path, therefore, the code under it could be imported directly.

after using : sys.path.append('F:ig dataPython_codingdiveintopython-5.4py')

I found I had this path inside sys.path: 'F:x08ig dataPython_codingdiveintopython-5.4py'

I then tested using the following code:mypath1='F:ig dataython_codingaiveintopython-5.4 y'

the mypath1 now is : 'F:x08ig datax08ython_codingx07iveintopython-5.4 y'

all the '' changed into 'x08' and 'a' changed into 'x07'

I searched for a while, but still can not find the reason, could you please check it out and any feedback or help will be appropriated. Many thanks.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Your strings are being escaped. Check out the docs on string literals:

The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter r' orR'; such strings are called raw strings and use different rules for backslash escape sequences.

This is a historical usage dating from the early 60s. It allows you to enter characters that you're not otherwise able to enter from a standard keyboard. For example, if you type into the Python interpreter:

print "xDC"

...you'll get ü. In your case, you have - representing backspace - which Python displays in the xhh form, where hh is the hexadecimal value for 08. a is the escape sequence for the ASCII bell: try print "a" with your sound on and you should hear a beep.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...