跳到主要内容

Python ord() 函数

ord() 函数返回代表 Unicode 字符的整数。

示例

character = 'P'

# 查找 P 的 Unicode
unicode_char = ord(character)
print(unicode_char)

# 输出:80

ord() 语法

ord() 的语法是:

ord(ch)

ord() 参数

ord() 函数接受单个参数:

  • ch - 一个 Unicode 字符

ord() 返回值

ord() 函数返回代表 Unicode 字符的整数。

示例:ord() 在 Python 中如何工作?

print(ord('5')) # 53
print(ord('A')) # 65
print(ord('$')) # 36

输出

53
65
36

顺便说一句,ord() 函数是 Python chr() 函数 的反函数。