chr(i)
返回整数 i 所对应的 Unicode 字符。如 chr(97)
返回字符串 a
,chr(8364)
返回字符串 €
。它的功能与 ord()
函数相反。
说明
参数 i 为整数,取值范围必须在 0 - 1114111(十六进制为 0x10FFFF)之间,否则将引发 ValueError 错误。
示例
>>> chr(8364)
'€'
>>> chr(97)
'a'
>>> chr('8364') # 参数 *i* 必须为整数
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>> chr(-1) # 取值范围必须在 [0, 1114111]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)
>>> chr(1114112) # 取值范围必须在 [0, 1114111]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)
>>> chr(0)
'\x00'
>>> chr(99)
'c'
>>> ord('c') # 功能与 `ord()` 函数相反
99
网友评论