ord()函数以及Python中的示例
Pythonord()功能
ord()函数是Python中的一个库函数,它用于从给定的字符值中获取数字值,它接受一个字符并返回一个整数,即用于将字符转换为整数,即用于获取a的ASCII值。给定的字符。
语法:
ord(character)
参数:character-要转换为整数值的字符值。
返回值:str–返回给定字符的整数值。
示例
Input: val = 'A' print(ord(val)) Output: 65
Python代码可将字符转换为整数(ASCII值)
#python代码演示一个例子 # of ord() function val = 'A' print("ASCII code of ", val, " is = ", ord(val)) val = 'x' print("ASCII code of ", val, " is = ", ord(val)) val = '@' print("ASCII code of ", val, " is = ", ord(val)) val = '8' print("ASCII code of ", val, " is = ", ord(val))
输出结果
ASCII code of A is = 65 ASCII code of x is = 120 ASCII code of @ is = 64 ASCII code of 8 is = 56