I want to do something like this.
example_string = "test"
print(example_string.replace(example_string[0], "b"))
expecting the output
best
however because the letter "t" is being passed into the method the t at the end also gets replaced with a "b". Resulting in the output
besb
How do I do it in a way that would result in the output "best" instead of "besb"?
new_str = "b" + example_string[1:].replace(a,b)is used for swapping all occurrences of stringawithb.word = "test"; i = word.index('t', 2); word[0:i] + "b" + word[i+1:]. Of course if you know the id of the char you want to replace instead of a letter you can directly set the variablei. If you need to substitue strings you could adapt it using thei+len(substring)+1to determine where the string ends.