What is the difference between .replace and re.sub? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between .replace and re.sub?

Are they the same? Why would I use re.sub after import the module, and not just use "Something".replace? For example: What is the difference between .replace re.sub? import re str = "My name is David. Hi David." pattern = r"David" newstr = re.sub(pattern, "Amy", str) print(newstr) ================================ newstr2 = str.replace("David", "Amy") print (newstr2)

29th Jan 2018, 8:21 AM
Juca Navaz Reque
Juca Navaz Reque - avatar
1 Answer