1
2# Python3 program to demonstrate the
3# use of replace() method
4
5string = "geeks for geeks geeks geeks geeks"
6
7# Prints the string by replacing geeks by Geeks
8print(string.replace("geeks", "Geeks"))
9
10# Prints the string by replacing only 3 occurrence of Geeks
11print(string.replace("geeks", "GeeksforGeeks", 3))
1# string.replace(old, new, count), no import is necessary
2text = "Apples taste Good."
3print(text.replace('Apples', 'Bananas')) # use .replace() on a variable
4Bananas taste Good. <---- Output
5
6print("Have a Bad Day!".replace("Bad","Good")) # Use .replace() on a string
7Have a Good Day! <----- Output
8
9print("Mom is happy!".replace("Mom","Dad").replace("happy","angry")) #Use many times
10Dad is angry! <----- Output
1string = "[Message]"
2string = string.replace("[", "")#Removes all [
3string = string.replace("]", "")#Removes all ]
4
5print(string)