I have a list, say named mac
, which saves 6-bytes mac address. I want to set the last byte to 0, then I use:
mac[5] = 0
But it gives me error:
TypeError: 'str' object does not support item assignment
How to fix this error?
because mac
is a str
and strings are immutable in python.
mac = list(mac)
mac[5] = '0'
mac = ''.join(mac) #to get mac in str
or use bytearray
, which could function as a mutable string.
>>> x = bytearray('abcdef')
>>> x
bytearray(b'abcdef')
>>> x[5] = '0'
>>> x
bytearray(b'abcde0')
>>> str(x)
'abcde0'
mac = mac[:5] + '\x00'
is what I need. Thank you anyway. – TieDad Apr 18 '13 at 01:24