2016-03-25 135 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