问题描述
显然,以下是有效的语法:
apparently, the following is the valid syntax:
my_string = b'the string'
我想知道:
- 字符串前面的这个b字符是什么意思?
- 使用有什么效果?
- 在什么情况下使用合适?
我在 so 上找到了一个相关问题,但这个问题是关于 php 的,它说明了 b 用于指示字符串是二进制的,与 unicode 不同,unicode 是与 php 版本兼容所必需的.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.
我确实在 python 网站上找到了关于使用u 字符以相同的语法将字符串指定为 unicode.不幸的是,它没有在该文档的任何地方提及 b 字符.
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' 的前缀在蟒蛇2;它表明文字应该成为字节文字在 python 3 中(例如,当代码用 2to3 自动转换).一个'u' 或 'b' 前缀可以后跟'r' 前缀.
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 文档 指出:
字节文字总是以'b'或'b'为前缀;它们生成 bytes 类型而不是 str 类型的实例.它们可能只包含 ascii 字符;数值为 128 或更大的字节必须用转义符表示.
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.