python - 二进制串、字节串、unicode串和普通串(str)的区别

标签 python

我有点困惑。在 Python 中,二进制字符串、字节字符串、unicode 字符串和普通旧字符串 (str) 之间有什么区别?我正在使用 Python 2.6。

最佳答案

这取决于您使用的 Python 版本。

在 Python 2.x 中,如果您编写 'abc',它的类型为 str,但这意味着一个字节串。如果你想要一个 Unicode 字符串,你必须写 u'abc'

在 Python 3.x 中,如果您编写 'abc',它仍然具有 str 类型,但现在这意味着它是一个 Unicode 字符的字符串。如果你想要一个字节串,你必须写b'abc'。不允许写u'abc'

        |  2.x                     |  3.x
--------+--------------------------+-----------------------
Bytes   |  'abc' <type 'str'>      |  b'abc' <type 'bytes'>
Unicode | u'abc' <type 'unicode'>  |   'abc' <type 'str'>

关于python - 二进制串、字节串、unicode串和普通串(str)的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8219706/

相关文章:

python - 我的正则表达式没有捕捉到文本中所需的模式?

python - 从外部打印到Docker容器内的交互式终端

python - 数据迁移导致错误AttributeError : type object 'User' has no attribute 'normalize_username' ?

Python- Selenium : Scraping AngularJS elements with loop over find_elements_by()

python - (集)多个值的理解

python - 用于整数特征的 NLTK 分类器?

php - Sublime Text 2 使用了错误的 PHP 版本

python - 在 Windows 下如何共享日志文件?

python - 错误: 'ColumnTransformer' object has no attribute '_n_features'

python - 从本地驱动器打开视频文件作为缓冲区并将其写入新文件