python中ord什么意思_Python ord()用法及代码示例

Python中内置的ord()函数

例子:

Input:a

Output:97

给定一个长度为1的字符串,当参数为unicode对象时,返回一个整数,该整数表示字符的Unicode代码点;如果参数为8位字符串,则返回字节的值。例如,ord('a')返回整数97,ord('€')(欧元符号)返回8364。这是8位字符串的chr()和Unicode对象的unichr()的逆。如果指定了unicode参数,并且Python是使用UCS2 Unicode构建的,则字符的代码点必须在[0..65535]范围内。

如果字符串长度大于一,则将引发TypeError。

语法可以是ord(“a”)或ord(‘a’),两者的结果相同。

# inbuilt function return an

# integer representing the Unicode code

value = ord("A")

# writing in ' ' gives the same result

value1 = ord('A')

# prints the unicode value

print value, value1

输出:

65 65

异常

1. TypeError:字符串长度大于1时引发。

# inbuilt function return an

# integer representing the Unicode code

# demonstrating exception

# Raises Exception

value1 = ord('AB')

# prints the unicode value

print (value1)

运行时错误:

Traceback (most recent call last):

File "/home/f988dfe667cdc9a8e5658464c87ccd18.py", line 6, in

value1 = ord('AB')

TypeError:ord() expected a character, but string of length 2 found

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值