Find Shortest Word

Find shortest word in string.

Logic BuildingIntermediate
Logic Building
# Take string input
s = input("Enter a string: ")

# Find shortest word
words = s.split()
if len(words) > 0:
    shortest = min(words, key=len)
    print(f"Shortest word: {shortest}")
else:
    print("No words found")

Output

Enter a string: Hello World Programming
Shortest word: Hello

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

Key Concepts:

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