The bytes.removeprefix()
method returns the bytes[len(prefix):]
if the binary data starts with the prefix
string. Otherwise, the original binary data is returned.
New in Python 3.9!
bytes.removeprefix(prefix, /)
It takes the following argument value:
prefix
: This shows the defined prefix that needs to be removed.The bytes.removeprefix()
method returns a bytes object, bytes[len(prefix):]
.
In the code snippets below, we are going to discuss whether the given object contains 'Test'
bytes or not.
# bytes type objectmessage = b'Test your coding skills with Educative!'# invoking removeprefix() to remove Test prefixprint(message.removeprefix(b'Test'))
'Test your coding skills with Educative!'
string as bytes.removeprefix()
method to remove the prefix 'Test'
bytes from the bytes object we created above. This returns a bytes object that contains b' your coding skills with Educative!'
.# Created a bytes type objectmessage= b'I love to Code!'# invoking removeprefix() to remove Code prefixprint(message.removeprefix(b'Code'))
'I love to Code!'
string as bytes.removeprefix()
method to remove the prefix 'Code'
bytes from the bytes object we created above. If the binary data does not contain the given prefix, then the method returns the bytes object as it is.