Find Shortest Word in Python

Find the shortest word in a sentence.

BeginnerString ProgramsExample 15 of 25
find-shortest-word.py
Run in browser
1# Program to find the shortest word in a sentence
2
3sentence = input("Enter a sentence: ")
4
5words = sentence.split()
6
7if not words:
8 print("No words found.")
9else:
10 shortest = min(words, key=len)
11 print("Shortest word:", shortest)

Output

Enter a sentence: this is a test
Shortest word: a

What's going on

We use min(words, key=len) to get the shortest word by length.