Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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 

PYTHON




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

Time complexity: O(n), where n is the number of characters in the text file.

Auxiliary space: O(1).

My Personal Notes arrow_drop_up
Last Updated : 20 Feb, 2023
Like Article
Save Article
Similar Reads
Related Tutorials