Skip to main content

Function display_words() in python to read lines from a text file, and display those words, which are less than 4 characters.

· 2 min read
Kaustubh Kulkarni
file.py
# Write a function display_words() in python to read lines from a text file, and display those words, which are less than 4 characters*
file.py
**def** wordSizeChecker(file_name,wordSize):
count=0
file=open(file_name)
lines=file.readlines()
**for** line **in** lines:
words=line.split()
**for** word **in** words:
**if** (len(word){} , Length : **{}**".format(word,str(len(word))))
wordSizeChecker("file.txt",4)

Output:

Output

Word : sit , Length : 3
Word : sed , Length : 3
Word : do , Length : 2
Word : ut , Length : 2
Word : et , Length : 2
Word : A , Length : 1
Word : at , Length : 2
Word : at. , Length : 3
Word : non , Length : 3
Word : sit , Length : 3
Word : id , Length : 2
Word : ut , Length : 2
Word : Leo , Length : 3
Word : ut , Length : 2
Word : sit , Length : 3
Word : sit , Length : 3
Word : et , Length : 2
Word : leo , Length : 3
Word : ut , Length : 2
Word : sed , Length : 3
Word : id , Length : 2