The next built-in type to discuss in Python is the bytearray type. It is very similar to the bytes type that was just previously discussed, except that it is a mutable, or changeable, sequence of integers, representing bytes typically from something like a string. You could create an empty array with that bytearray function. You could create an array that will be filled with nulls. If you use bytearray and an integer, there will be 11 elements initialized to a byte value of 0, or a null value. You can also specify the bytes that you want in the array as a sequence of integers. So using the bytearray constructor function, with those integers, will then create an array of them.
# The bytearray class provides a mutable sequence # Values must be integers from 0-255 to represent a byte empty_array = bytearray() null_array = bytearray(11) ints_array =bytearray((84, 114, 97, 100, 101, 109, 97, 114, 107, 32, 194, 174)) str_array = bytearray('Trademark ®', 'utf-8') str_array =bytearray(b'Trademark ® \xc2\xae') print('bytes_array =', bytes_array) print('bytes_array.decode() ->', bytes_array.decode()) str_literal ='Trademark ®' # A bytearray sequence behaves similar to a string print('str_literal.count("T") ->', str_literal.count('T')) print('str_literal.index("T") ->', str_literal.index('T')) # However, byte values are used instead of string values print('bytes_array.count(0x54) ->', bytes_array.count(0x54)) print('bytes_array.index(0x54) ->', bytes_array.index(0x54)) # Bytearray objects have methods to mutate them bytes_array.append(32) print('bytes_array =', bytes_array) bytes_array.extend((194, 174)) print('bytes_array =', bytes_array) print('bytes_array.decode() ->', bytes_array.decode()) bytes_array.remove(0x54) print('bytes_array=',bytes_array) bytes_array.insert(0, 0x54) print('bytes_array =',bytes_array) bytes_array.pop() bytes_array.pop() print('bytes_array.decode() ->', bytes_array.decode()) The Output: bytes_array = bytearray(b'Trademark ®\xc2\xae') bytes_array.decode() -> Trademark ® str_literal.count("T") -> 1 str_literal.index("T") -> 0 bytes_array.count (0x54) -> 1 bytes_array.index(0x54) -> 0 bytes_array = bytearray(b'Trademark\xc2\xae') bytes_array = bytearray(b'Trademark \xc2\xae \xc2\xae') bytes_array = bytearray.decode() -> Trademark ® ® bytes_array =bytearray(b'Trademark \xc2\xae \xc2\xae') bytes_array.decode() –Trademark ®
Similar to the bytes type object, you can initialize your array based upon an existing string that you have, and the encoding that that string is in. And finally, you can also create a bytearray object based upon a bytes object. In this case, a literal bytes object is used to create that bytes_array. So in the first printout, we see the way that a bytearray is represented with bytearray, parenthesis, and then what looks like a literal bytes object. Like the bytes object, it does have a decode method, which defaults to UTF-8. You can also specify the decoding format that you want to use. In this case, it decodes to trademark, with that registered trademark symbol.
With the bytes_array append method, you could append a single byte. Where the byte value 32 represents a space, it’s a little hard to see in the printout, but there is a space right before the end of that string, here. You could also extend a bytes_array object with multiple bytes. In this case, those two bytes that represent the one single registered trademark symbol are being added with the extend method. And so we see those two additional bytes encoded in hexadecimal. When you use decode method on a bytes_array, again, you could see how that might be represented in UTF-8 as the registered trademark.
# Bytearray objects have methods to mutate them bytes_array.append(32) print('bytes_array=', bytes_array) bytes_array.extend((194, 174)) print('bytes_array =',bytes_array) print('bytes_array.decode() ->', bytes_array.decode()) The output : bytes_array = bytearray(b'Trademark \xc2\xae') bytes_array = bytearray(b'Trademark \xc2\xae \xc2\xae') bytes_array =bytearray.decode() -> Trademark ® ®
By using a remove method, we’re able to take that byte out of the array. Remember, hexadecimal 54 was the capital T. After that’s executed, and we print the object again, you can see the capital T is no longer present. You could insert at a specific position. In this case, at position 0, add a byte with hexadecimal value 54, would reinsert that capital T that was previously removed. With the pop method, it will normally remove from the end of the string. Although not demonstrated, you could use an argument to specify the index position that you want to pop off. So by using pop twice, the last two bytes were removed from the string, which removed, essentially, just that one registered trademark symbol. You can see that when we print out the decoded version of bytes_array. So by this demonstration, you should now know how to work with the bytearray object, and understand its similarity to the bytes object, and to the string object as well.
bytes_array.remove(0x54) print('bytes_array=', bytes_array) bytes_array.insert(0, 0x54) print('bytes_array =', bytes_array) bytes_array.pop() bytes_array.pop() print('bytes_array.decode() ->',bytes_array.decode()) The output: bytes_array =bytearray(b'Trademark \xc2\xae \xc2\xae') bytes_array.decode() –Trademark ®