Python Program to Read a Text File and Count the Number of Words whose Length is Greater than 5

Python Program to Read a Text File and Count the Number of Words whose Length is Greater than 5

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 read a text file and then we have to count the number of words present in it whose length in greater than 5.

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 Read a Text File and Count the Number of Words whose Length is Greater than 5


Python Program to Read a Text File and Count the Number of Words whose Length is Greater than 5


Logic and Explanation

Question - Write a Python Program to Read a Text File and Count the Number of Words whose Length is Greater than 5

Python Program to Read a Text File and Count the Number of Words whose Length is Greater than 5 - In this program we have to first read the text file word by word. Suppose you are given a text file "content.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("content.txt", "r") as f:
    data = f.read()


Now, the next thing we will have to do is to iterate through the data string word by word and at each step of iteration we will be checking for the length of each word using conditional statements. Also, we will create a count variable in which we will update the value by one each time we get a word whose length is grater than five.



That's all the process we will be following to solve this question. I hope this logic part is clear to you and now let's write the code.

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

count = 0
for word in data.split():
    if len(word) > 5:
        count += 1
print("No of Word whose length greater than 5", count)


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) As we are using with open() to open a text file in Python, there is no need to close the file separately. It auto closes the file.

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





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 (0)

Previous Post Next Post