python基础--函数2(ascii,bin,ord,chr,oct,hex)

1. 内容简介:

本节介绍一些常用的函数:ascii,bin,ord,chr,oct,hex。

2. 测试代码:

1).ascii(x):

返回一个可打印的对象字符串方式表示,如果是非ascii字符就会输出\x,\u或\U等字符来表示。与python2版本里的repr()是等效的函数。

>>> ascii(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> ascii('a')
"'a'"
>>> ascii(10)
'10'
>>> ascii('10')
"'10'"
>>>

2).bin(x):

将一个整形数字转换成二进制字符串。

>>> bin(8)
'0b1000'
>>> bin(8.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
>>>

说明:bin函数的参数必须为整型。

3).ord(x):

返回Unicode字符对应的整数。

>>> ord('a')
97
chr:返回整数所对应的Unicode字符
>>> chr(97) #参数类型为整数
'a'

4).oct(x)

将整数转化成8进制数字符串。

>>> oct(10)
'0o12'

5).hex(x)
hex:将整数转换成16进制字符串。

>>> hex(15)
'0xf'

进制转换,你学到了么?

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值