Count Each Character
Count frequency of each character.
BeginnerPhase 5 - Category 1: Basic String HandlingExample 10 of 10
count-each-character.py
1# Take string input2s = input("Enter a string: ")34# Count characters5freq = {}6for char in s:7 freq[char] = freq.get(char, 0) + 189# Display10for char, count in sorted(freq.items()):11 print(f"{char}: {count}")
Output
Enter a string: hello e: 1 h: 1 l: 2 o: 1
What's going on
Use dictionary to count frequency.
Key Concepts:
Use dictionary to track counts
freq.get(char, 0) returns count or 0
Increment for each occurrence
🔗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