2016-03-25 134 views
3

在Python 3字符串中Unicode characters是什么意思?python的字符串是unicode字符

因为Python 3.0,语言特性包含 Unicode字符的STR类型,这意味着使用创建的任何字符串“unicode的石头!”, “unicode的石头!”,或三引号字符串语法存储为 Unicode

from python doc。

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

是否unicode字符表示unicode codepoints?

+0

为什么这么重要? – hop

+0

只是好奇吗? :) – eugene

+0

@hop因为它改变了你如何处理字符串。 –

回答

0

是否unicode字符是指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