python - Python 3.x 中字符串的内部表示是什么

标签 python string unicode python-3.x

在 Python 3.x 中,字符串由 Unicode 序数项组成。 (请参阅下面语言引用中的引文。)Unicode 字符串的内部表示是什么?是 UTF-16 吗?

The items of a string object are Unicode code units. A Unicode code unit is represented by a string object of one item and can hold either a 16-bit or 32-bit value representing a Unicode ordinal (the maximum value for the ordinal is given in sys.maxunicode, and depends on how Python is configured at compile time). Surrogate pairs may be present in the Unicode object, and will be reported as two separate items.

最佳答案

内部表示将在 Python 3.3 中改变,它实现了 PEP 393 .新的表示会选择ascii, latin-1, utf-8, utf-16, utf-32中的一个或几个,一般都是为了得到一个紧凑的表示。

只有在与旧版 API 对话时才会隐式转换为代理对(这些 API 仅存在于 windows 上,其中 wchar_t 是两个字节); Python 字符串将被保留。这里是release notes .

关于python - Python 3.x 中字符串的内部表示是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1838170/

相关文章:

python - 如何将 PyFrameObject 转换为 PyObject

c# - 如何从字符串中删除所有非数字字符并仅将数字作为新字符串获取?

java - 根据位置替换字符串?

Ruby:如何仅从 unicode 文本中分解打印连字?

Python unicode 正则表达式匹配因某些 unicode 字符而失败 - 错误或错误?

python - 当从命令行传入时,是否有一种Python式的方式为变量赋值?

Python + celery : Prevent duplicate task execution

python - 避免比较数字和字符串的错误

Python os.path.join Linux 上的绝对路径

c++ - 使用 imread 打开具有 unicode 名称的图像文件