Python 内建函数 - bytes([source[,encoding[,errors]]])

Manual

Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256. bytes is an immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.

Accordingly, constructor arguments are interpreted as for bytearray().

Bytes objects can also be created with literals, see String and Bytes literals.

See also Binary Sequence Types — bytes, bytearray, memoryview, Bytes, and Bytes and Bytearray Operations.

直译

返回新的bytes对象,其为 0 <= x < 256区间内的不可变整数序列。bytes是bytearray的不可变版本,它有相同的非变异方法和相同的索引及切片行为。构造器参数解析方法与bytearray()相同。

实例

>>> bytes('CSDN', 'utf-8')
b'CSDN'
>>> bytes(17)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> bytes(range(10))
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t'
>>> bytes(i for i in range(2, 21) if i % 2 == 0)
b'\x02\x04\x06\x08\n\x0c\x0e\x10\x12\x14'

Note

注意与bytearray()输出结果的区别

拓展阅读

string和bytes字面
二进制序列类型 — bytes, bytearray, memoryview
Bytes
Bytes和Bytearray操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值