欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python_ord_函数_2021.6.5

程序员文章站 2022-04-27 18:40:46
...

函数ord(x)是返回字符x对应的Unicode编码
#注意看清楚是字符,不是字符串

给数字会报错

>>> ord(5)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    ord(5)
TypeError: ord() expected string of length 1, but int found

给字符串也会报错

>>> ord('56')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    ord('56')
TypeError: ord() expected a character, but string of length 2 found

给单字符不会报错

>>> ord('5')
53
相关标签: python二级