bytes([source[, encoding[, errors]]]) 创建一个不可变的字节数组
bytearray([source[, encoding[, errors]]]) 创建一个可变的字节数组
参数的意义比较复杂,需要参照标准库。
比较常用的两个
一个整形的参数代表创建一个字节数组, n个长度的数组 bytes(n)
bytearry(string, 'utf-8')将字符串编码为utf-8的字节码数组
chr(97) = a 字符转unicode编码
ord(a) = 97 unicode编码转字符
ord函数:为序数(order)函数,函数返回值为字符在ASCII码中的序号。
ascii(object) 同repr函数,输出对象的represention对象,只是会将ascii不能显示的字符,用用\x十六进制,或者\u(Unicode编码表示)
class A:
def __repr__(self):
return "hello■"
def __str__(self):
return "str■"
a = A()
print(a)
print(ascii(a))输出结果为:
str■
hello\u25a0
hex(x)将整形的数据转化为16进制字符串
enumerate(iterable, start=0) ,将迭代内容,以start为开始,枚举为元组
lt = ['ok', 'he2', 'he3']
for e in enumerate(lt, 3):
print(e)输出结果为
(3, 'ok')
(4, 'he2')
(5, 'he3')
迭代
all() 迭代对象中的所有数据单位都是True,等价于如下代码:def all(iterable):
for element in iterable:
if not element:
return False
return True any() 迭代对象中存在任何一个对象为True
<完>
本文介绍 Python 中字节序列、字符编码转换、数据类型转换等基础操作,包括 bytes 和 bytearray 的使用方法,以及 chr、ord、ascii、bin 和 hex 函数的应用。同时涵盖了 enumerate 在迭代中的使用技巧及 all 和 any 函数的功能。
1006

被折叠的 条评论
为什么被折叠?



