该函数是一个类对象:

class bytes([source[,encoding[,errors]]])

返回值为字节对象,当第一个参数为字符串时,必须提供第二个参数,第二个参数为编码类型的字符串。

bytes()返回对象中的元素是不可修改的。

下面看看例子:

>>> bytes('中国','utf-8')
b'\xe4\xb8\xad\xe5\x9b\xbd'
>>> bytes([1,3,4])
b'\x01\x03\x04'
>>> a = bytes([1,3,4])
>>> a[2]
4
>>> a[2] = 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bytes' object does not support item assignment

 

相关文章:

  • 2022-01-07
  • 2021-10-02
  • 2021-06-08
猜你喜欢
  • 2021-11-07
  • 2022-12-23
  • 2021-12-15
  • 2022-02-14
  • 2021-12-22
  • 2022-12-23
  • 2022-01-26
相关资源
相似解决方案