What does the #39;b#39; character do in front of a string literal?(字符串文字前面的“b字符有什么作用?)
问题描述
显然,以下是有效的语法:
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.
这篇关于字符串文字前面的“b"字符有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:字符串文字前面的“b"字符有什么作用?


- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01