Find Longest Word
Find longest word in string.
IntermediatePhase 5 - Category 3: Word OperationsExample 4 of 10
find-longest-word.py
1# Take string input2s = input("Enter a string: ")34# Find longest word5words = s.split()6if len(words) > 0:7 longest = max(words, key=len)8 print(f"Longest word: {longest}")9else:10 print("No words found")
Output
Enter a string: Hello World Programming Longest word: Programming
What's going on
Use max() with key=len to find longest.
Key Concepts:
max() finds maximum
key=len uses length for comparison
Returns longest 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