Python program: Find the longest word
Find the longest word in a sentence.
The code
sentence = "Python is an extraordinarily readable programming language" words = sentence.split() longest = max(words, key=len) print(longest, len(longest)) print(sorted(words, key=len, reverse=True)[:3])
Output
extraordinarily 15 ['extraordinarily', 'programming', 'readable']
How it works
max with key=len compares words by their length instead of alphabetically.
Python features used
Learn the ideas behind it in the free lesson More Functions.