gpt4 book ai didi

python的字符串是unicode字符

转载 作者:行者123 更新时间:2023-11-28 19:13:45 25 4
gpt4 key购买 nike

python 3字符串中的Unicode字符是什么意思?

Since Python 3.0, the language features a str type that contain Unicode characters, meaning any string created using "unicode rocks!", 'unicode rocks!', or the triple-quoted string syntax is stored as Unicode

来自 python 文档。

对于字符串 abc,Python 是否在内存中保存 [61, 62, 63]?(因为 a 是 U+0061)

unicode字符是指unicode代码点吗?

最佳答案

Does unicode character mean unicode codepoints?

是也不是。这取决于 python 的版本及其构建方式。

对于 2.2 到 3.2 版本,python 支持窄和宽 unicode 构建(参见 PEP-261 )。在狭窄的构建中,unicode 范围仅限于 BMP :

Python 3.2.6 (default, Feb 21 2016, 12:42:00)
[GCC 5.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> sys.maxunicode
65535

因此超出此范围的字符必须表示为 surrogate pair :

>>> s = '😬'
>>> ord(s)
128556
>>> len(s)
2

随着PEP-0393的引入, python3 不再支持窄构建,因此一个字符始终等同于一个代码点:

Python 3.5.1 (default, Mar 3 2016, 09:29:07)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sys.maxunicode
1114111
>>> s = '😬'
>>> ord(s)
128556
>>> len(s)
1

关于python的字符串是unicode字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com