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'
进制转换,你学到了么?