字符串文字中的反斜杠是什么意思? [英] What does a backslash mean in a string literal?

查看:734
本文介绍了字符串文字中的反斜杠是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我列出了这样的列表:

list=["\0","\1","\2","\3","\4","\5","\6","\7","\8","\9","\10","\11"]

但是如果我打印列表,则输出如下:

but if I print the list, the output is like this:

['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\\8', '\\9', '\x08', '\t']

我不知道为什么会这样.列表中"\"有什么具体含义吗?

I don't have any idea why this happens. Is there any specific meaning of "\" in a list?

推荐答案

反斜杠用于转义字符串文字中的特殊(不可打印)字符. \n用于换行,\t用于制表符,\f用于换页(很少使用),并且还有更多.

The backslash is used to escape special (unprintable) characters in string literals. \n is for newline, \t for tab, \f for a form-feed (rarely used) and several more exist.

当您给字符串原义词"\0"时,您实际上表示的是一个只有一个字符的字符串,该字符是(不可打印的)NUL字符(0字节).您可以在字符串文字中将其表示为\0. \1(字符串中的1个字节)等也是如此.

When you give the string literal "\0" you effectively denote a string with exactly one character which is the (unprintable) NUL character (a 0-byte). You can represent this as \0 in string literals. The same goes for \1 (which is a 1-byte in a string) etc.

实际上,\8\9是不同的,因为在反斜杠之后,您必须用八进制表示法表示想要的字节值,例如e. G.仅使用数字07.如此有效地,8之前和9之前的反斜杠没有特殊含义,并且\8产生两个字符,即逐字反斜杠和8作为数字逐字逐字.

Actually, the \8 and \9 are different because after a backslash you have to denote the value of the byte you want in octal notation, e. g. using digits 07 only. So effectively, the backslash before the 8 and before the 9 has no special meaning and \8 results in two characters, namely the backslash verbatim and the 8 as a digit verbatim.

当您现在打印这样的字符串文字的表示形式时(例如,通过将其打印在列表中),Python解释程序将为内部字符串重新创建一个表示形式(应该看起来像是字符串文字).这不是字符串的内容,而是您可以在Python程序i中表示的字符串的版本. e.括在引号中,并使用反斜杠转义特殊字符.但是,Python解释器不使用八进制表示法表示特殊字符.它使用十六进制表示法来代替,该符号在每个特殊字符前都添加一个\x,后面紧跟两个十六进制字符.

When you now print the representation of such a string literal (e. g. by having it in a list you print), then the Python interpreter recreates a representation for the internal string (which is supposed to look like a string literal). This is not the string contents, but the version of the string as you can denote it in a Python program, i. e. enclosed in quotes and using backslashes to escape special characters. The Python interpreter doesn't represent special characters using the octal notation, though. It uses the hexadecimal notation instead which introduces each special character with a \x followed by exactly two hexadecimal characters.

这意味着\0变为\x00\1变为\x01等.如上所述,\8实际上是两个字符的表示,即反斜杠和数字8.然后,Python解释程序将反斜杠转义为双反斜杠\\,并将8附加为普通字符.

That means that \0 becomes \x00, \1 becomes \x01 etc. The \8, as mentioned, is in fact the representation of two characters, namely the backslash and the digit 8. The backslash is then escaped by the Python interpreter to a double backslash \\, and the 8 is appended as normal character.

输入\10是具有值8的字符(因为八进制10是十进制8且也是十六进制8,请查找八进制和十六进制数字以了解有关信息).因此,输入\10变为\x08. \11是值为9的字符,它是制表符,具有特殊符号,即\t.

The input \10 is the character with value 8 (because octal 10 is decimal 8 and also hexadecimal 8, look up octal and hexadecimal numbers to learn about that). So the input \10 becomes \x08. The \11 is the character with value 9 which is a tab character for which a special notation exists, that is \t.

这篇关于字符串文字中的反斜杠是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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