+ 1

i can not understand how replace function work ??

3rd May 2017, 11:23 PM
morad saadallah
morad saadallah - avatar
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.
3rd May 2017, 11:38 PM
Ulisses Cruz
Ulisses Cruz - avatar
- 1
You can leave off the optional third parm to replace all occurrences
4th May 2017, 1:42 AM
Ed H.
Ed H. - avatar