Find Shortest Word
Find shortest word in string.
IntermediatePhase 5 - Category 3: Word OperationsExample 5 of 10
find-shortest-word.py
1# Take string input2s = input("Enter a string: ")34# Find shortest word5words = 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
🔗Related Content
- 📖
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- 💻
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum