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操作