Count Words
Count number of words in string.
BeginnerPhase 5 - Category 1: Basic String HandlingExample 7 of 10
count-words.py
1# Take string input2s = input("Enter a string: ")34# Count words5words = s.split()6count = len(words)78print(f"Number of words: {count}")
Output
Enter a string: Hello World Programming Number of words: 3
What's going on
Split string and count elements.
Key Concepts:
split() splits by whitespace
Returns list of words
Count list length
🔗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