显然,以下是有效的语法:
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"字符有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)