Longest and shortest word
Write a Python program to find the longest and the shortest word in a string.
"Python makes string handling easy"
Longest: handling Shortest: easy
Approach
- Split into words and use
max/minwithkey=len.
Solution
text = "Python makes string handling easy"words = text.split()print("Longest:", max(words, key=len))print("Shortest:", min(words, key=len))Longest: handling Shortest: easy
Line by line
key=len compares words by their length; on a tie, the first such word wins.
Learn the concept
New to this topic? Read the lesson, then come back.