Python | Finding ‘n’ Character Words in a Text File

This article aims to find words with a certain number of character. In the code mentioned below, a python program is given to find the words contain three characters in the text file.

Examples:

Input : Hello, how are you ?

Output :
how
are
you

Code: Python program to find the words contain three characters in the text file

filter_none

edit
close

play_arrow

link
brightness_4
code

count = 1
chrw = ""
  
# text file
file = open('textfile.txt', 'r')
while 1:
    sp = file.read(1)
  
    if count<= 3:
        chrw = chrw + sp
  
    if count>3:
        if sp ==" ":
            count = 0
            if len(chrw)>0:
                print(chrw)
                chrw =""
        elif sp !=" ":
            chrw =""
    count = count + 1
  
    if not sp:
        break
  
file.close() 

chevron_right


Output:

how 
are 
you
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.