How do I 'declare' an empty bytes variable?

Solution 1:

Just use an empty byte string, b''.

However, concatenating to a string repeatedly involves copying the string many times. A bytearray, which is mutable, will likely be faster:

msg = bytearray()  # New empty byte array
# Append data to the array
msg.extend(b"blah")
msg.extend(b"foo") 

To decode the byte array to a string, use msg.decode(encoding='utf-8').

Solution 2:

Use msg = bytes('', encoding = 'your encoding here').

Encase you want to go with the default encoding, simply use msg = b'', but this will garbage the whole buffer if its not in the same encoding