How can I open a file, Stud.txt, and then replace any occurences of "A" with "Orange"?
The Ctrl + F and Command + F keyboard shortcut keys also work in Microsoft Excel and other spreadsheet programs to open the Find and Replace text box. In Microsoft Excel, older versions featured the Edit menu, and the Replace option is found in that menu.
Replace a specific string in a list. If you want to replace the string of elements of a list, use the string method replace() for each element with the list comprehension. If there is no string to be replaced, applying replace() will not change it, so you don't need to select an element with if condition .
with open("Stud.txt", "rt") as fin: with open("out.txt", "wt") as fout: for line in fin: fout.write(line.replace('A', 'Orange'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With