Count Consonants
Count consonants with a character loop. Students require a letter before excluding vowels and see the English-vowel rule applied to Unicode alphabetic text.
1# Take string input2s = input("Enter a string: ")34# Count consonants5vowels = "aeiouAEIOU"6count = 07for char in s:8 if char.isalpha() and char not in vowels:9 count += 11011print(f"Number of consonants: {count}")
Output
Enter a string: Hello Number of consonants: 3
In this one
- Filter alphabetic nonvowels
- Exclude punctuation
- Understand the chosen vowel set
How it works
- 1
Visit each character.
- 2
Require isalpha and absence from vowels.
- 3
Increment the consonant count for each match.
Edge cases
Do spaces count as consonants?
Do spaces count as consonants?
No. They fail isalpha.
Does uppercase B count?
Does uppercase B count?
Yes. It is alphabetic and not in the vowel list.
What's going on
The condition combines char.isalpha() with char not in vowels. That prevents digits and punctuation from being counted merely because they are not vowels.
Every qualifying letter adds one to count. Both English cases are covered by the vowel string. isalpha recognizes more than English letters, so alphabetic characters outside that vowel list fall into this program's consonant category even when linguistically imperfect.
Do spaces count as consonants? No. They fail isalpha.
Does uppercase B count? Yes. It is alphabetic and not in the vowel list.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why include isalpha?
Pick an answer to check it.
Question 2
Do spaces count as consonants?
Pick an answer to check it.
Questions people ask
Do spaces count as consonants?
No. They fail isalpha.
Does uppercase B count?
Yes. It is alphabetic and not in the vowel list.
Why include isalpha?
Nonvowels are not necessarily letters.
๐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