Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to delete the last byte of a bytes in python?

Tags:

python

For some reason, I have to remove the last byte of a bytes. For example,

teststr = b'\x01\x02\x03'

In my case, I have to remove the last byte \x03. I know the strip can do in the python string case, but it doesn't work in the bytes case.

Is there a way to do this? Any information is appreciated.

like image 629
Touchstone Avatar asked Sep 16 '25 00:09

Touchstone


1 Answers

You can use slicing:

teststr[:-1]

is equal to:

b'\x01\x02'
like image 139
niekas Avatar answered Sep 18 '25 17:09

niekas