Find Shortest Word

Find the shortest word in a sentence.

BeginnerTopic: String Programs
Back

Python Find Shortest Word Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to find the shortest word in a sentence

sentence = input("Enter a sentence: ")

words = sentence.split()

if not words:
    print("No words found.")
else:
    shortest = min(words, key=len)
    print("Shortest word:", shortest)
Output
Enter a sentence: this is a test
Shortest word: a

Understanding Find Shortest Word

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

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents