Find Shortest Word

Find shortest word in string.

IntermediatePhase 5 - Category 3: Word OperationsExample 5 of 10
find-shortest-word.py
1# Take string input
2s = input("Enter a string: ")
3
4# Find shortest word
5words = s.split()
6if len(words) > 0:
7 shortest = min(words, key=len)
8 print(f"Shortest word: {shortest}")
9else:
10 print("No words found")

Output

Enter a string: Hello World Programming
Shortest word: Hello

What's going on

Use min() with key=len to find shortest.

Key Concepts:

min() finds minimum
key=len uses length for comparison
Returns shortest word