ord()
函数是Python的内置函数,用于返回单个字符的Unicode编码,它接受一个字符串作为参数,如果参数为空或者不是字符串类型,则会抛出异常。
ord(x)
x:表示要获取Unicode编码的字符。
返回一个整数,表示字符x的Unicode编码。
下面通过几个示例来演示ord函数的使用方法:
1、示例1:获取字符’A’的Unicode编码
print(ord('A')) # 输出:65
2、示例2:获取数字字符’0’的Unicode编码
print(ord('0')) # 输出:48
3、示例3:获取中文字符’中’的Unicode编码
print(ord('中')) # 输出:20013
4、示例4:获取空格字符的Unicode编码
print(ord(' ')) # 输出:32