Python 3 How do I 'declare' an empty `bytes` variable Python 3 How do I 'declare' an empty `bytes` variable python-3.x python-3.x

Python 3 How do I 'declare' an empty `bytes` variable


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 arraymsg.extend(b"blah")msg.extend(b"foo") 

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


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


As per documentation:

Blockquote socket.recv(bufsize[, flags]) Receive data from the socket. The return value is a string representing the data received. Blockquote So, I think msg="" should work just fine:

>>> msg = "">>> msg''>>> len(msg)0>>>