Strings in Python are immutable meaning you cannot replace parts of them.
You can however create a new string that is modified. Mind that this is not semantically equivalent since other references to the old string will not be updated.
You could for instance write a function:
def replace_str_index(text,index=0,replacement=''):
return '%s%s%s'%(text[:index],replacement,text[index+1:])
or since the introduction of f-strings:
def replace_str_index(text,index=0,replacement=''):
return f'{text[:index]}{replacement}{text[index+1:]}'
And then for instance call it with:
new_string = replace_str_index(old_string,middle)
If you do not feed a replacement, the new string will not contain the character you want to remove, you can feed it a string of arbitrary length.
For instance:
replace_str_index('hello?bye',5)
will return 'hellobye'
; and:
replace_str_index('hello?bye',5,'good')
will return 'hellogoodbye'
.
slice_before_index + char + slice_after_index
.