bytearray和bytes不一样的地方在于,bytearray是可变的。
str = '人生苦短,我用Python!'
bytes = bytearray(str.encode())
bytes = bytearray(b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!')
str = bytes.decode()
print(str)
改变bytearray
bytes[:6] = bytearray('生命'.encode())
bytes = bytearray(b'\xe7\x94\x9f\xe5\x91\xbd\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8Python!')
str = bytes.decode()
print(str)