Find Longest Word inside Text File Using Python 3

In this tutorial, I’ll teach you how to find the longest word inside a text file using Python 3. You need to provide a test.txt file that contains the text you want to check.

def longest_word(filename):
    with open(filename, 'r') as infile:
              words =
    max_len = len(max(words, key=len))
    return [word for word in words if len(word) == max_len]


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.