How to Find the Longest Words in a Text File using Python?

In this article, we will show you how to find and print all the longest words from a given text file using Python. The longest words are those having the same length as the maximum length word in the file.

We'll demonstrate this with a sample text file containing various words of different lengths.

Sample Text File

Let's create a sample text file to work with ?

# Create a sample text file
sample_text = """Good Morning Tutorials Point
This is Tutorials Point sample File
Consisting of Specific
abbreviated
source codes in Python Seaborn Scala
Imagination
Summary and Explanation
Welcome user
Learn with a joy"""

with open('sample_file.txt', 'w') as file:
    file.write(sample_text)

print("Sample file created successfully!")
Sample file created successfully!

Algorithm Steps

Following are the steps to find the longest words ?

  • Open the text file in read mode using open() function

  • Read the file content and split it into words using split() function

  • Find the length of the longest word using len() and max() functions

  • Use list comprehension to collect all words with maximum length

  • Display the results

Implementation

# Read the text file and find longest words
with open('sample_file.txt', 'r') as file_data:
    # Read file content and split into words
    words_list = file_data.read().split()
    
    # Find the length of the longest word
    longest_word_length = len(max(words_list, key=len))
    
    # Get all words with maximum length using list comprehension
    longest_words = [word for word in words_list if len(word) == longest_word_length]
    
    print(f"Longest word length: {longest_word_length}")
    print("The longest words from the text file:")
    for word in longest_words:
        print(f"- {word}")
Longest word length: 11
The longest words from the text file:
- abbreviated
- Imagination
- Explanation

Alternative Approach Using Counter

You can also group words by length and find the maximum length group ?

from collections import defaultdict

with open('sample_file.txt', 'r') as file_data:
    words_list = file_data.read().split()
    
    # Group words by their length
    words_by_length = defaultdict(list)
    for word in words_list:
        words_by_length[len(word)].append(word)
    
    # Find maximum length
    max_length = max(words_by_length.keys())
    
    print(f"Maximum word length: {max_length}")
    print(f"Longest words: {words_by_length[max_length]}")
Maximum word length: 11
Longest words: ['abbreviated', 'Imagination', 'Explanation']

Handling Edge Cases

Here's a more robust version that handles empty files and punctuation ?

import re

def find_longest_words(filename):
    try:
        with open(filename, 'r') as file_data:
            # Remove punctuation and split into words
            content = file_data.read()
            words_list = re.findall(r'\b[a-zA-Z]+\b', content)
            
            if not words_list:
                return "No words found in the file."
            
            # Find longest word length
            longest_length = len(max(words_list, key=len))
            
            # Get all longest words (remove duplicates)
            longest_words = list(set([word for word in words_list 
                                    if len(word) == longest_length]))
            
            return longest_words, longest_length
            
    except FileNotFoundError:
        return "File not found."

# Test the function
result = find_longest_words('sample_file.txt')
if isinstance(result, tuple):
    words, length = result
    print(f"Longest words ({length} characters): {words}")
else:
    print(result)
Longest words (11 characters): ['Explanation', 'abbreviated', 'Imagination']

Conclusion

Finding the longest words in a text file involves reading the file, splitting content into words, and using Python's max() function with key=len. List comprehension provides an efficient way to filter words by length. Handle edge cases like empty files and punctuation for robust solutions.

Updated on: 2026-03-26T21:26:36+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements