python string find substring and replace it code example
Example 1: python replace substring
txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x) # ----> I like apples
Example 2: python replace matching string
s = 'one two one two one'
# 1st argument: string to find
# 2nd argument: string to put in place
print(s.replace(' ', '-')) # one-two-one-two-one