?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)
具體參考Python字節數組【bytes/bytearray】 - fieldtianye - 博客園