Which approach should I take? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Which approach should I take?

If I want to replace a word in a sentenance with another word. So should I take the approach of a for loop for that or .sub() method?

4th Oct 2018, 4:27 PM
Aditya Taggar
Aditya Taggar - avatar
3 Answers
+ 1
I would use: string = string.replace("word1", "word2")
5th Oct 2018, 10:47 PM
HonFu
HonFu - avatar
+ 1
Thanks. I never knew about this method.
6th Oct 2018, 1:17 PM
Aditya Taggar
Aditya Taggar - avatar
0
I think the .sub() method is better due to its easy and short syntax. But at first it is confusing.
4th Oct 2018, 4:29 PM
Aditya Taggar
Aditya Taggar - avatar