KnowledgeBoat Logo

Computer Science

Write a Python function that finds and displays all the words longer than 5 characters from a text file "Words.txt".

Python File Handling

1 Like

Answer

Suppose the file "Words.txt" contains the following text:

The quick brown fox jumps over the lazy dog and runs swiftly through the forest
def display():
    file = open("Words.txt", 'r')
    content = file.read()
    words = content.split()
    for word in words:
        if len(word) > 5:
            print(word, end = ' ')
    file.close()
display()
Output
swiftly through forest 

Answered By

1 Like


Related Questions