当前位置: 首页 > 图灵资讯 > 技术篇> Python 函数 —— ord(), chr()

Python 函数 —— ord(), chr()

来源:图灵教育
时间:2023-06-07 09:39:04

chr(i)

Return the string representing a character whose Unicode code point is the integer i. For example, chr(97) returns the string ‘a’, while chr(8364) returns the string ‘€’. This is the inverse of ord().

The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in base 16). ValueError will be raised if i is outside that range.

In [4]: chr(14)Out[4]: '\x0e'In [5]: chr(44)Out[5]: ','In [6]: chr(60)Out[6]: '<'

ord(c)

Given a string representing one Unicode character, return an integer representing the Unicode code point of that character. For example, ord(‘a’) returns the integer 97 and ord(‘€’) (Euro sign) returns 8364. This is the inverse of chr().

In [7]: ord('a')Out[7]: 97In [8]: chr(97)Out[8]: 'a'