Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 26 Mar, 2020

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




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

Output:

how 
are 
you

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!