Python Practice › File Handling Programs › Program 9
Longest word in a file
Write a Python program to find the longest word in a file.
Output
experimentation
Approach
- Split the text into words and use
max(key=len).
Solution
with open("story.txt", "w") as f: f.write("Python makes automation\nand experimentation enjoyable\n") with open("story.txt") as f: words = f.read().split()print(max(words, key=len))Output
experimentation
Learn the concept
New to this topic? Read the lesson, then come back.