'Remove all bytes after null byte (\x00) [duplicate]

I have a python client that receives a string from a C server. I always receive a 6 byte string, in C everything after the '\0' byte is junk, so on my client I need to remove all the bytes after that null byte. How exactly can I do that on a byte object? I tried converting it to string and then removing it but it didn't seem to work

I have something like this b'str\x00...' where ... represents the junk bytes



Solution 1:[1]

bytes have a .split() method like str:

>>> b = b'str\x00...'
>>> b.split(b'\x00')[0]
b'str'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wjandrea