bytes doesn't support item deletion because it's immutable. To "modify" strings and string-like objects you need to take a copy, so to remove olddata[start:end] do:

newdata = olddata[:start] + olddata[end:]

Of course that's a fair amount of copying, not all of which is necessary, so you might prefer to rework your code a bit for performance. You could use bytearray (which is mutable). Or perhaps you could find a way to work through the buffer (using an index or iterating over its elements), instead of needing to shorten it after each step.

Answer from Steve Jessop on Stack Overflow
🌐
Python.org
discuss.python.org › python help
Strip byte string and take only importante values - Python Help - Discussions on Python.org
July 7, 2023 - Hello all…good day…please help on how to strip byte string as below: input : b'\x081F304984\x0843501' output : 1F304984 thanks a lot
Discussions

python - How to remove some bytes from a byte string? - Stack Overflow
I am trying to remove a byte (\x00\x81) from a byte string sByte. sByte = b'\x00\x81308 921 q53 246 133 137 022 1 0 1 1 1 130 C13 330 0000000199 04002201\n' I am expecting to have as a result the More on stackoverflow.com
🌐 stackoverflow.com
How to remove first 4 bytes from s string in python - Stack Overflow
I got a special packet in string format, which has 32 bytes header and the body contains one of more entries, each consist of 90 bytes. I want to process this string using python. Can I just read ... More on stackoverflow.com
🌐 stackoverflow.com
string - Delete some specific content from byte in python 3 - Stack Overflow
Since the character you want to remove is specifically at the end and the object supports the method, the solution is the same as in striping characters from the end of a string. Just make sure to pass the desired characters are bytes. >>> my_bytes = b'blah\x00\x00\x00' >>> my_bytes.rstrip(b'\x00') b'blah' ... In Python ... More on stackoverflow.com
🌐 stackoverflow.com
Delete non-decodeable chars in string?
There is no way to know if a byte read is actually supposed to be a character or just coincidentally looks like an encoded code point. Your s is a unicode (utf16 if I'm not mistaken) string because you defined it as a string. If you actually read a bytes object from file the situation is different. Still, so many byte sequences are correct utf8 encoded unicode characters that you are unlikely to filter much. Just reading a PDFs bytes and treating it as text is not reasonable. More on reddit.com
🌐 r/learnpython
9
1
March 7, 2024
🌐
Educative
educative.io › answers › what-is-the-bytes-removeprefix-method-in-python
What is the bytes removeprefix() method in Python?
New in Python 3.9! ... 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.
Top answer
1 of 2
3

Use bytes.replace to replace the substring with an empty string:

b = b'Today, in the digital age, any type of data, such as text, images, and audio, can be\r\ndigitized, stored indefinitely, and transmitted at high speeds. Notwithstanding these\r\nadvantages, digital data also have a downside. They are easy to access illegally, tamper\r\nwith, and copy for purposes of copyright violation.\r\nThere is therefore a need to hide secret identification inside certain types of digital\r\ndata. This information can be used to prove copyright ownership, to identify attempts\r\nto tamper with sensitive data, and to embed annotations. Storing, hiding, or embedding\r\nsecret information in all types of digital data is one of the tasks of the field of\r\nsteganography.\r\nSteganography is the art and science of data hiding. In contrast with cryptography,\r\nwhich secures data by transforming it into another, unreadable format, steganography\r\nmakes data invisible by hiding (or embedding) them in another piece of data, known\r\nalternatively as the cover, the host, or the carrier. The modified cover, including the\r\nhidden data, is referred to as a stego object. It can be stored or transmitted as a message.\r\nWe can think of cryptography as overt secret writing and of steganography as covert\r\nsecret writing.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

b = b.replace(b'\x00', b'')
assert b.endswith(b'writing.')
2 of 2
2

Bytes objects behave like many other iterables, which means slicing and indexing should work as expected. Since the character you want to remove is specifically at the end and the object supports the method, the solution is the same as in striping characters from the end of a string. Just make sure to pass the desired characters are bytes.

>>> my_bytes = b'blah\x00\x00\x00'
>>> my_bytes.rstrip(b'\x00')
b'blah'
🌐
Educative
educative.io › answers › what-is-the-bytes-removesuffix-method-in-python
What is the bytes removesuffix() method in Python?
The bytes.removesuffix() method returns the bytes[:-len(suffix)] if the binary data ends with the suffix string and is not empty. Otherwise, the original binary data is returned. bytes[:-len(suffix)] means it will return bytes data from
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › delete non-decodeable chars in string?
r/learnpython on Reddit: Delete non-decodeable chars in string?
March 7, 2024 -

Hello - i would like to delete all non-decodable chars from a string - and i tried it with the following code but his is not working -

s = "this  ง, ญ, ณ, น, ม, ร, ล, ฬ is a text ���}��j)���.ߪs*i� ��zmj��q��p with something between"
line = bytes(s, 'utf-8').decode('utf-8', 'ignore')
print(s)
print(line)

This ���}� chars are read from a pdf / doc / exe file or something like that and i would like to delete this information from ths string but keep everything else in the string (so the english, but also the thai-chars).

How can i do this and clean the string?

🌐
Python Forum
python-forum.io › thread-38829.html
extract only text strip byte array
November 29, 2022 - first of, awesome forum. you guys have been super helpful to a noob.. without guilting me into 'reading the manual' this is how i learn, and glad you are helping me learn.... i have various byte strings with text in them.. the bytes are alway...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-remove-b-prefix-from-string
How to remove the 'b' prefix from a String in Python | bobbyhadz
Use the `bytes.decode()` method to remove the `b` prefix from a bytes object by converting it to a string.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.4 documentation
The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped. ... The outermost leading and trailing chars argument values are stripped from the string.