20
Phase 5 - Category 2: Counting & Analysis
Chapter 20 • Intermediate
75 min
Phase 5 - Category 2: Counting & Analysis
Introduction
Learn to analyze strings by counting different types of characters, finding frequencies, and extracting statistical information.
Key Concepts
Character Counting
- Vowels & Consonants: Count letters by type
- Digits, Letters, Special: Categorize characters
- Uppercase/Lowercase: Count by case
- Frequency Analysis: Count occurrences of each character
Character Classification
- Vowels: a, e, i, o, u (and uppercase)
- Consonants: All other letters
- Digits: 0-9
- Special Characters: Everything else
String Methods
char.isalpha(): Check if letterchar.isdigit(): Check if digitchar.isupper(): Check if uppercasechar.islower(): Check if lowercasechar.isspace(): Check if space
Common Patterns
Count Vowels
python.js
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
Frequency Dictionary
python.js
freq = {}
for char in text:
freq[char] = freq.get(char, 0) + 1
Problem-Solving Approach
- Iterate: Loop through each character
- Classify: Determine character type
- Count: Increment appropriate counter
- Store: Use dictionary for frequency analysis
Hands-on Examples
Count Vowels and Consonants
# Take string input
text = input("Enter a string: ")
# Count vowels and consonants
vowels = "aeiouAEIOU"
vowel_count = 0
consonant_count = 0
for char in text:
if char.isalpha(): # Only count letters
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
print(f"Vowels: {vowel_count}")
print(f"Consonants: {consonant_count}")Iterate through string, check if character is letter. If vowel, increment vowel count; otherwise increment consonant count.
Related Tutorials
🔗Related Content
- 💻
Phase 5 - Practice Problems
Practice Phase 5 concepts with hands-on coding problems
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with assessment questions
- ➡️
Phase 6 - Get Started
Continue to Phase 6 after mastering Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum