字符串文字前面的“b"字符有什么作用?

2022-01-09 00:00:00 python unicode string binary

问题描述

显然,以下是有效的语法:

Apparently, the following is the valid syntax:

my_string = b'The string'

我想知道:

  1. 字符串前面的这个b字符是什么意思?
  2. 使用有什么效果?
  3. 在什么情况下使用合适?

我在 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.

另外,出于好奇,有没有比 bu 更多的符号来做其他事情?

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.

相关文章