Q. Write a method/function DISPLAYWORDS() in python to read lines from a text file STORY.TXT, and display those words, which are less than 4 characters.


You can understand by Watching video :-



Answer :-

def DISPLAYWORDS() :
    file = open("story.txt", "r")
    lst = file.readlines()
    for i in lst :
        word = i.split()
        for j in word :
            if len( j ) < 4 :
                print( j )
    file.close()

print("Word with length smaller than 3 :- \n")
DISPLAYWORDS()

Story.txt Contains :-

A boy is playing there.
There is a playground.
An aeroplane is in the sky.
Alphabets & numbers are allowed in password.
This is Path Walla Website.

Output :-

Word with length smaller than 3 :-

A
boy
is
is
a
An
is
in
the
&
are
in
is

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post