+ 1
i can not understand how replace function work ??
2 Respuestas
+ 10
imagine you have:
s = "I like Python."
You want to replace the word 'like' to 'hate'. You do:
s.replace('like', 'hate')
print(s) # output: I hate Python
Now imagine:
s = "programming is challenging and challenging is good."
Now you want to replace any occurence of 'is' for 'was'. You do:
s.replace('is', 'was', 2) # the third argument is the number of ocurrences to replace.
Hope this helps.
- 1
You can leave off the optional third parm to replace all occurrences