Python Program to Reverse Each Word in a Text File

Python program to reverse each word in a text file

In this article, we will be solving a very interesting question of data file handling with python based on text file. According to the question we have to reverse each and every word in a text file. For example if the word is "hello" so it should be changed to "olleh".

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python program to reverse each word in a text file


Python Program to Reverse Each Word in a Text File


Logic and Explanation

Question - Write a Python program to reverse each word in a text file

Python program to reverse each word in a Text File - In this program we have reverse each and every word present in a text file. Suppose you are given a text file "demo.txt". So, first thing we will be doing is to opening the text file in read mode and reading the data of the text file and storing it in string.

with open("demo.txt", "r") as f:
    data = f.read()


Now this data variable contains the whole text present in this demo.txt file. So, we will iterate through this string word by word and then in each step of iteration we will reverse the word. Also, here we will create another variable new_data and will concatenate the reversed word in it. Finally we will overwrite this new_data variable in the text file.


I hope the logic and process we will be following to solve the question in clear to you. Now you can check the code for the given problem. If any doubt arises in your mind related to the logic, feel free to ask in the comments section below.

new_data = ""
for word in data.split():
    rev_word = word[::-1]
    new_data += rev_word
    new_data += " "

with open("demo.txt","w") as f:
    f.write(new_data)


Code Insights

1) As we wanted to iterate word by word through the data, we are splitting the data with space. You can see that there is nothing written inside the split(), this is because by default it splits the string with a space.

2) Here, we are using the concept of string slicing to reverse the word. You can also reverse it by iterating through letter by letter for each word in reverse order and then adding it to string variable.

3) In the 5th line, in above code block you can see we are adding a space to the new_data variable. This is because each word should be separated should be separated by a space in text file.

4) At end as we wanted to finally replace the old data of the file with the new data containing reversed word, we are opening the text file in write mode. This will overwrite the data in the text file.

Also Read - Write a Python Program to Write 1 to 100 in a Text File


I hope the logic and the code behind each and every step of the program is clear to you. You can check the complete source code here.

#Reading the given text file

with open("demo.txt", "r") as f:
    data = f.read()

new_data = ""

#Reading the data and reversing each word

for word in data.split():
    rev_word = word[::-1]
    new_data += rev_word
    new_data += " "

#Writing the new data in text file

with open("demo.txt","w") as f:
    f.write(new_data)


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





1 Comments

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

Previous Post Next Post