Longest word in a file

EasyFile Handling Programs · 9 of 38

Write a Python program to find the longest word in a file.

Advertisement
Output
experimentation

Approach

  1. Split the text into words and use max(key=len).

Solution

Python
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
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs