美文网首页
Python内置函数chr()

Python内置函数chr()

作者: 简书冷雨 | 来源:发表于2017-09-15 09:51 被阅读0次

chr(i)

返回整数 i 所对应的 Unicode 字符。如 chr(97) 返回字符串 achr(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

相关文章

网友评论

      本文标题:Python内置函数chr()

      本文链接:https://www.haomeiwen.com/subject/wnudsxtx.html