temp_str = 'this is a test'
print(temp_str.replace('is','IS')
print(temp_str)
#################### Result ###########################
thIS IS a test
this is a test
#our text
text='Hello there how may I help you!'
#replacing ("THIS" with "THIS")
replaced_text=text.replace("!", "?")
print(replaced_text)
#output
"Hello there how may I help you?"
# Syntax - str.replace(old, new [, count])
song = 'cold, cold heart'
replaced_song = song.replace('o', 'e')
# The original string is unchanged
print('Original string:', song)
print('Replaced string:', replaced_song)
song = 'let it be, let it be, let it be'
# maximum of 0 substring is replaced
# returns copy of the original string
print(song.replace('let', 'so', 0))
# replace in Python, works like search and replace in word processor
greet = 'Hello Bob'
nstr1 = greet.replace('Bob', 'Jane')
print(nstr1) # Output: Hello Jane
# Initial string doesn't change
print(greet)
# replaces all occurrences of the sub string
nstr2 = greet.replace('o', 'X')
print(nstr2) # Output: HellX BXb
# We can do replace only for number of times
nstr3 = greet.replace('o', 'O', 1)
print(nstr3) # Output: HellO Bob
# Syntax
string.replace(old, new, count)
# old – old substring you want to replace.
# new – new substring which would replace the old substring.
# count – the number of times you want to replace the old substring with the new substring. (Optional )
string = "geeks for geeks geeks geeks geeks"
print(string.replace("geeks", "Geeks"))
print(string.replace("geeks", "GeeksforGeeks", 3))
# Output
# Geeks for Geeks Geeks Geeks Geeks
# GeeksforGeeks for GeeksforGeeks GeeksforGeeks geeks geeks