May 10, 2021 Python2
We know that the string inside python cannot be modified directly from the original place, so in order to modify the original string, we have a method:
1. Singing and merge
>>> a='abcde'
>>> b='fghij'
>>> c=a[1:3]+b[2:5]+' end'
>>> c
'bchij end'
>>>
2. replace()
>>> a='aaabbbcccddd'
>>> a.replace ('aaa','xxx')
'xxxbbbcccddd'
>>>
3. Combine find() and shrapned
>>> a='aaaxbbbcccxddd'
>>> where = a.find ('x')
>>> where
3
>>> a[:where]+'ttttt'+a[where:]
'aaatttttxbbbcccxddd'
>>>
The three methods above, although all of which modify the source string, actually do not modify it directly in place, but simply re-create a new string object
4. Use a modifiable list
Maybe you need to modify the extra-long text in multiple places, when the above method is not performing well, so you need to convert to an object-list that can be modified
>>> a='aaaxbbbcccxddd'
>>> b=list(a)
>>> b
['a', 'a', 'a', 'x', 'b', 'b', 'b', 'c', 'c', 'c', 'x', 'd', 'd', 'd']
>>> b[2]='x'
>>> b[7]='x'
>>> b
['a', 'a', 'x', 'x', 'b', 'b', 'b', 'x', 'c', 'c', 'x', 'd', 'd', 'd']
>>> a=''.join (b)
>>> a
'aaxxbbbxccxddd'
>>>