def replace_at_index(string, index):
print (string.replace(string[index], "-", 1))
That's my current code for replacing a character with a given index.
Not sure why it's doing this. The result I'm wanting is for it to replace the given Index, which in the case of "Housee" Index 5 would be "House-"
This is a hack but works:
def replace_at_index(string, index):
ls = list(string)
ls[index] = "-"
s = "".join(ls)
print(s)
Look at the function doc-string: string.replace(old, new, count)
so it replaces count as many occurences that it can find.
You cannot change a string. Each string is newly created, it is immutable. What you want is:
def replace_at_index(string, index):
string = string[:index] + "-" + string[index+1:]
return string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With