原因:python 字符串赋值意外地将 '\b' 更改为 '\x08' 并将 '\a' 更改为 '\x07',为什么 Python 会这样做? [英] the reason: python string assignments accidentally change '\b' into '\x08' and '\a' into '\x07', why Python did this?

查看:137
本文介绍了原因:python 字符串赋值意外地将 '\b' 更改为 '\x08' 并将 '\a' 更改为 '\x07',为什么 Python 会这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个答案和一些评论,提到了另一个问题,但都没有提供 REASON,为什么 Python 会发生这种变化?比如 '/b' is '/x08' 只是结果,但为什么呢?干杯.

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.

我尝试添加这个路径F:\big data\Python_coding\diveintopython-5.4\py"进入sys.path,因此可以直接导入其下的代码.

I try to add this path"F:\big data\Python_coding\diveintopython-5.4\py" into sys.path, therefore, the code under it could be imported directly.

使用后:sys.path.append('F:\big data\Python_coding\diveintopython-5.4\py')

我发现我在 sys.path 中有这个路径:'F:\x08ig data\Python_coding\diveintopython-5.4\py'

I found I had this path inside sys.path: 'F:\x08ig data\Python_coding\diveintopython-5.4\py'

然后我使用以下代码进行了测试:mypath1='F:\big data\bython_coding\aiveintopython-5.4\ry'

I then tested using the following code:mypath1='F:\big data\bython_coding\aiveintopython-5.4\ry'

mypath1 现在是:'F:\x08ig data\x08ython_coding\x07iveintopython-5.4\ry'

the mypath1 now is : 'F:\x08ig data\x08ython_coding\x07iveintopython-5.4\ry'

所有的'\b'都变成了'\x08','\a'变成了'\x07'

all the '\b' 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.

推荐答案

您的字符串正在被转义.查看关于字符串文字的文档:

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

反斜杠 () 字符用于转义字符否则有特殊含义,例如换行符,反斜杠本身,或引号字符.字符串文字可以有选择地加前缀用字母 r' 或R';这样的字符串称为原始字符串并使用反斜杠转义序列的不同规则.

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.

这是 60 年代初的历史用法.它允许您输入无法从标准键盘输入的字符.例如,如果您在 Python 解释器中输入:

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"

...你会得到Ü.在您的情况下,您有 \b - 表示退格 - Python 以 \xhh 形式显示,其中 hh 是 08 的十六进制值. \a 是 ASCII 铃声的转义序列:尝试 print "\a" 并打开声音,您应该会听到哔哔声.

...you'll get Ü. In your case, you have \b - 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.

这篇关于原因:python 字符串赋值意外地将 '\b' 更改为 '\x08' 并将 '\a' 更改为 '\x07',为什么 Python 会这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆