Bytestrings (and strings in general) are immutable objects in Python. By creating them, you cannot change them. Instead, you need to create a new one that has some of the old content. (For example, with a baseline, newString = oldString[:offset] + newChar + oldString[offset+1:] or the like.)
Instead, you may need to convert bytestring to a list of bytes first or bytearray, manipulate it, and then convert bytearray / list back to a static string after all the manipulations are done. This allows you to create a new line for each replacement operation.
source share