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>>> a = '&#'
2>>> print a.replace('&', r'\&')
3\&#
4>>> print a.replace('#', r'\#')
5&\#
6>>>
7