在python3中,bytes和str是不相同的两种类型
bytes拼接
In [16]: b1 = bytes("hello", 'utf-8')
In [17]: b1
Out[17]: b'hello'
In [18]: b2 = bytes('world', 'utf-8')
In [19]: b2
Out[19]: b'world'
In [20]: type(b1)
Out[20]: bytes
In [21]: type(b2)
Out[21]: bytes
In [22]: b3 = b1 + b2
In [23]: b3
Out[23]: b'helloworld'
In [24]: s1 = b3.decode('utf-8')
In [25]: s1
Out[25]: 'helloworld'
In [26]: type(s1)
Out[26]: str
本文详细介绍了Python3中bytes和str类型的使用区别,通过实例展示了如何将字符串转换为字节串,以及如何将字节串解码回字符串。了解这些基础知识对于Python编程至关重要。
1605





