, , strip() , rstrip() , strip() , rstrip() .
However, by default, NUL characters are not treated as strip () as white space characters, so you need to specify them explicitly. This may catch you, since print (), of course, will not show NUL characters. My solution that I used was to clear the string using " .strip().strip('\x00')":
>>> arbBytesFromSocket = b'\x00\x00\x00\x00hello\x00\x00\x00\x00'
>>> arbBytesAsString = arbBytesFromSocket.decode('ascii')
>>> print(arbBytesAsString)
hello
>>> str(arbBytesAsString)
'\x00\x00\x00\x00hello\x00\x00\x00\x00'
>>> arbBytesAsString = arbBytesFromSocket.decode('ascii').strip().strip('\x00')
>>> str(arbBytesAsString)
'hello'
>>>
This gives you the required string without NUL characters.
source
share