什么是“B”字符字符串文字的前面呢? [英] What does the 'b' character do in front of a string literal?
问题描述
显然,以下是有效的语法...
Apparently, the following is valid syntax...
my_string = b'The string'
我想知道......
I would like to know...
- 这是什么
B
盈字符的字符串意味着什么? - 什么是使用它的影响?
- 什么是用它适当的场合。
- What does this
b
character infront of the string mean? - What are the effects of using it?
- What are appropriate situations to use it.
我发现了一个相关问题就在这里SO但问题是关于PHP虽然并陈述了 b
用于指示该字符串是二进制的,而不是单向code这是需要code是从PHP&LT版本兼容; 6移植到PHP 6时,我不认为这也适用于Python的。
I found a related question right here on SO but that question is about PHP though and it states the b
is used to indicate the string is binary as opposed to unicode which was needed for code to be compatible from version of PHP < 6 when migrating to PHP 6. I don't think this applies to Python.
我没有找到蟒网站上的这个文档关于使用 U
在相同的语法字符来指定一个字符串作为单code。遗憾的是它并没有提到在乙字符的任意位置在该文件中
I did find this documentation on the python site about using a u
character in the same syntax to specify a string as unicode. Unfortunately it doesn't mention the b character anywhere in that document.
此外,只是出于好奇,有没有比更多符号B
和 U
即做其他事情?
Also, just out of curiosity, are there more symbols than the b
and u
that do other things?
推荐答案
要报价了Python 2 .X文档:
'b'或'B'的preFIX中被忽略
Python 2中;它表明
文字应该成为一个字节的文字
在Python 3(例如,当code是
与2to3的自动转换)。一个
'U'或'B'preFIX可以跟随
一个'R'preFIX。
A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.
借助的Python 3.3文档规定:
字节的文字总是prefixed以'b'或'B';他们生产字节的实例类型,而不是海峡的类型。他们可能只包含ASCII字符;具有128或更高的数值字节必须与逃逸pssed前$ P $。
Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.
这篇关于什么是“B”字符字符串文字的前面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!