Python: String replace index

you can do

s="cdabcjkewabcef"
snew="".join((s[:9],"###",s[12:]))

which should be faster than joining like snew=s[:9]+"###"+s[12:] on large strings


You can achieve this by doing:

yourString = "Hello"
yourIndexToReplace = 1 #e letter
newLetter = 'x'
yourStringNew="".join((yourString[:yourIndexToReplace],newLetter,yourString[yourIndexToReplace+1:]))

You can use join() with sub-strings.

s = 'cdabcjkewabcef'
sequence = '###'
indicies = (9,11)
print sequence.join([s[:indicies[0]-1], s[indicies[1]:]])
>>> 'cdabcjke###cef'