Unicode 编码
- python 中字符串都是用 Unicode 编码,几乎覆盖所有的字符。
- 在python中进行编码转换都是通过 unicode码 作为中间值实现的。
- unicode 有个 通用字符集,每个字符有个编码(code points);编码可以有多种形式实现,如 utf-8、utf-16 等。
- 从 0-1114111(0-0x10ffff) 空间或码位,每个编码对应一个字符。
>>> chr(10004)
'✔'
>>> for i in range(9800, 9800+12):
chr(i)
'♈'
'♉'
'♊'
'♋'
'♌'
'♍'
'♎'
'♏'
'♐'
'♑'
'♒'
'♓'
chr() 用法详解
- 将 Unicode 码位转化为 字符。
- 有些字符显示,有些字符要 print 之后显示,有些因为硬件配置问题不显示。
>>> help(chr)
Help on built-in function chr in module builtins:
chr(i, /)
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
>>> chr(1)
'\x01'
>>> print(chr(1))
>>> chr(10)
'\n'
ord() 用法详解
将 字符 转化为 Unicode 码位。
>>> help(ord)
Help on built-in function ord in module builtins:
ord(c, /)
Return the Unicode code point for a one-character string.
>>> ord('0')
48
>>> ord('?')
65311
>>> ord('一')
19968