1my_text = 'Aello world'
2my_text = my_text.replace(my_text[0], 'H')
3print (my_text)
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>>> x = 'xpple bxnxnx cherry'
2>>> a = x.replace(x,a) # replaces x with a
3'apple banana cherry'
4
5>>> first_a = x.replace(x,a,1) # only replaces first a
6'apple bxnxnx cherry'
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)