>>> def replace(string, slc_seq, rep):
... for slc in reversed(slc_seq):
... string[slc[0]:slc[1]] = rep
...
>>> a = bytearray('1 first 2 second 3', 'utf-8')
>>> a
bytearray(b'1 first 2 second 3')
>>>
>>> replace(a, ((2, 7), (10, 16)), b'asdfghasdfghasdfghbb')
>>> a
bytearray(b'1 asdfghasdfghasdfghbb 2 asdfghasdfghasdfghbb 3')
>>>
>>> b = bytearray('1 first 2 second 3', 'utf-8')
>>> b
bytearray(b'1 first 2 second 3')
>>>
>>> replace(b, ((2, 7), (10, 16)), b'abc')
>>> b
bytearray(b'1 abc 2 abc 3')
>>>