In pthon, how can I fix this code so that it changes the word 'language' in the file to test below where the user inputs info? ile=input('Enter a file name : ') string1=input('Enter a string to be replaced : ') string2=input('Enter a string to be added : ') with open(file,'r') as f: data=f.read() data=data.replace(string1,string2) with open(file,'w') as f: f.write(data) f = open(file, "r") print(f.read()) the file is called Problem 2.txt and contains the following text: Python language is incredibly easy to use and learn for new beginners and newcomers. The python language is one of the most accessible programming languages available because it has simplified syntax and not complicated, which gives more emphasis on natural language. Due to its ease of learning and usage, python codes can be easily written and executed much faster than other programming languages. right now, the text just displays: Enter a file name : problem 2.txt Enter a string to be replaced : language Enter a string to be added : test however, I need the output to look like this: Enter a file name : problem 2.txt Enter a string to be replaced : language Enter a string to be added : test Python test is incredibly easy to use and learn for new beginners and newcomers. The python test is one of the most accessible programming tests available because it has simplified syntax and not complicated, which gives more emphasis on natural test. Due to its ease of learning and usage, python codes can be easily written and executed much faster than other programming tests.
In pthon,
how can I fix this code so that it changes the word 'language' in the file to test below where the user inputs info?
ile=input('Enter a file name : ')
string1=input('Enter a string to be replaced : ')
string2=input('Enter a string to be added : ')
with open(file,'r') as f:
data=f.read()
data=data.replace(string1,string2)
with open(file,'w') as f:
f.write(data)
f = open(file, "r")
print(f.read())
the file is called Problem 2.txt and contains the following text:
Python language is incredibly easy to use and learn
for new beginners and newcomers. The python language
is one of the most accessible
available because it has simplified syntax and not
complicated, which gives more emphasis on natural
language. Due to its ease of learning and usage,
python codes can be easily written and executed much
faster than other programming languages.
right now, the text just displays:
Enter a file name : problem 2.txt
Enter a string to be replaced : language
Enter a string to be added : test
however, I need the output to look like this:
Enter a file name : problem 2.txt
Enter a string to be replaced : language
Enter a string to be added : test
Python test is incredibly easy to use and learn
for new beginners and newcomers. The python test
is one of the most accessible programming tests
available because it has simplified syntax and not
complicated, which gives more emphasis on natural
test. Due to its ease of learning and usage,
python codes can be easily written and executed much
faster than other programming tests.
Step by step
Solved in 4 steps with 4 images