# string.replace(old, new, count), no import is necessary
text ="Apples taste Good."print(text.replace('Apples','Bananas'))# use .replace() on a variable
Bananas taste Good.<---- Output
print("Have a Bad Day!".replace("Bad","Good"))# Use .replace() on a string
Have a Good Day! <----- Output
print("Mom is happy!".replace("Mom","Dad").replace("happy","angry"))#Use many times
Dad is angry! <----- Output
# Read in the filewithopen('file.txt','r')asfile:
filedata =file.read()# Replace the target string
filedata = filedata.replace('ram','abcd')# Write the file out againwithopen('file.txt','w')asfile:file.write(filedata)
temp_str ='this is a test'print(temp_str.replace('is','IS')print(temp_str)#################### Result ###########################
thIS IS a test
this is a test
# plz suscribe to my youtube channel --># https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A
text ="python is too difficult , I have a good experience with it"#python is too difficult I am using this text for example onlyprint(text.replace('difficult','easy'))#####output######python is too easy , I have a good experience with it
# Replace a particular item in a Python list
a_list =['aple','orange','aple','banana','grape','aple']for i inrange(len(a_list)):if a_list[i]=='aple':
a_list[i]='apple'print(a_list)# Returns: ['apple', 'orange', 'apple', 'banana', 'grape', 'apple']
# Syntax - str.replace(old, new [, count])
song ='cold, cold heart'
replaced_song = song.replace('o','e')# The original string is unchangedprint('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 stringprint(song.replace('let','so',0))
string ="Welcome to Softhunt website, Softhunt is a website dedicated to programming courses"print("Original String :", string)# replacing 'courses' with 'tutorials'print("Replaced String: ", string.replace("courses","tutorials"))# replacing only 2 occurences of 'Softhunt'print("Replaced with 2 occurences: ",string.replace("Softhunt","Softhunt.net",2))
# string.replace(old, new, count), no import is necessary
text ="Apples taste Good."print(text.replace('Apples','Bananas'))# use .replace() on a variable
Bananas taste Good.<---- Output
print("Have a Bad Day!".replace("Bad","Good"))# Use .replace() on a string
Have a Good Day! <----- Output
print("Mom is angry!".replace("Mom","Dad").replace("angry","happy"))#Use many times
Dad is angry! <----- Output
# 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
string ="Welcome to Softhunt website, Softhunt is a website dedicated to programming courses"print("Original String: ", string)# replacing s by eprint("Replaced String: ", string.replace("s","e"))# 3 occurrence of m by aprint("Replaced with 3 occurrences: ", string.replace("m","a",3))
import sys
import fileinput
x =input("Enter text to be replaced:")
y =input("Enter replacement text")for l in fileinput.input(files ="file.txt"):
l = l.replace(x, y)
sys.stdout.write(l)