bytes 创建 bytes 对象

内置函数(类)bytes,Python 官方文档描述如下:

help(bytes)
Help on class bytes in module builtins:

class bytes(object)
 |  bytes(iterable_of_ints) -> bytes
 |  bytes(string, encoding[, errors]) -> bytes
 |  bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
 |  bytes(int) -> bytes object of size given by the parameter initialized with null bytes
 |  bytes() -> empty bytes object
 |  
 |  Construct an immutable array of bytes from:
 |    - an iterable yielding integers in range(256)
 |    - a text string encoded using the specified encoding
 |    - any object implementing the buffer API.
 |    - an integer
 |  
 |  Methods defined here:
 |  

返回一个新的二进制序列 bytes 对象。参数可以是:

  • 0~255 的整数组成的可迭代类型
  • 字符串,并指定编码格式 encoding
  • 与缓冲区接口一致的对象
  • 整数
  • 或者不传参数
type(bytes)
type
bytes([1,2,3])
b'\x01\x02\x03'
bytes('嗨', 'utf-8')
b'\xe5\x97\xa8'
bytes(3)
b'\x00\x00\x00'
bytes()
b''