Count Vowels
Count English vowel characters in text with Python. Students traverse a string, test membership in both cases, and count occurrences not distinct letters.
1# Take string input2s = input("Enter a string: ")34# Count vowels5vowels = "aeiouAEIOU"6count = 07for char in s:8 if char in vowels:9 count += 11011print(f"Number of vowels: {count}")
Output
Enter a string: Hello Number of vowels: 2
In this one
- Test vowel membership
- Count occurrences
- Handle both English cases
How it works
- 1
Define the listed English vowels and initialize count.
- 2
Visit each source character.
- 3
Increment only for membership matches, then print.
Edge cases
Does uppercase E count?
Does uppercase E count?
Yes. E is listed in vowels.
Does y count?
Does y count?
No. It is absent from this vowel set.
What's going on
The vowel string lists a, e, i, o, u in lowercase and uppercase. Each source character is tested against it.
Every match adds one. For Banana, the three occurrences of a give a count of three, not one distinct vowel. Spaces and digits fail the membership test. Accented vowels and y are not included in this explicitly defined English set.
Does uppercase E count? Yes. E is listed in vowels.
Does y count? No. It is absent from this vowel set.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does the counter measure?
Pick an answer to check it.
Question 2
Does uppercase E count?
Pick an answer to check it.
Questions people ask
Does uppercase E count?
Yes. E is listed in vowels.
Does y count?
No. It is absent from this vowel set.
What does the counter measure?
Vowel occurrences.
๐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