I'm trying to overwrite a string with another string (from an index), in Python 3.x. I want to know if there is already a String method that can already do this or if there is an efficient way to do this.
I know the easiest way to achieve this would be to write a loop that starts at an index and then replaces each character of string a with string b (shown in code below).
def func(a, b, index):
newStr = list(a)
for i in range(index, index+len(b)):
if i < len(a):
newStr[i] = b[i-index]
else:
newStr.append(b[i-index])
return ''.join(newStr)
If I have two strings string:
a = '012345678' and b = 'abcde'
And the index set as 6, then the expected output from the function should be: '012345abcde'.
a[:6] + b