beginner

Count Vowels

Count the number of vowels in a string.

Vowel counting is a fundamental string processing exercise that helps you master character iteration, conditional logic, and membership testing. These skills are essential for text analysis and natural language processing tasks.

📚 Concepts & Theory

Counting vowels in a string is a classic string processing problem that teaches iteration, conditional logic, and character checking. Vowels are the letters: a, e, i, o, u (and sometimes y).

Key Concepts:

1. Character Iteration
Loop through each character in the string:

for char in text:
# process each character

2. Membership Testing
Check if a character exists in a collection:

vowels = "aeiou"
if char in vowels:
# char is a vowel

Approach 1: Counter with Loop

count = 0
for char in text.lower():
if char in "aeiou":
count += 1

Approach 2: List Comprehension with sum()

count = sum(1 for char in text.lower() if char in "aeiou")

Approach 3: Using Counter from collections

from collections import Counter
vowel_count = sum(Counter(text.lower())[v] for v in "aeiou")

Important Considerations:

  • Case sensitivity: "A" vs "a" - use .lower() to normalize

  • Non-alphabetic characters: numbers and symbols are not vowels

  • Accented characters: é, à, etc. (basic version ignores them)
The simple counter approach is most readable and efficient for this task.

🎯 Your Challenge

Write count_vowels that returns the vowel count.

📝 Starter Code

Python
def count_vowels(text):
    pass

print(count_vowels('hello world'))
  • Create a counter variable starting at 0
  • Loop through each character in the string
  • Convert characters to lowercase for comparison
  • Check if each character is in the vowels string: aeiou
  • Increment your counter when you find a vowel

Solution

Python
def count_vowels(text):
    vowels = 'aeiouAEIOU'
    count = 0
    for char in text:
        if char in vowels:
            count += 1
    return count

print(count_vowels('hello world'))

Explanation

This solution iterates through each character in the string after converting it to lowercase (to handle both upper and lowercase vowels). For each character, it checks if the character exists in the string "aeiou" using the membership operator (in). When a vowel is found, the counter is incremented. The use of .lower() ensures case-insensitive counting. Time complexity: O(n) where n is the string length. Space complexity: O(1) as we only store a counter.

⚠️ Common Mistakes to Avoid

  • Forgetting to handle uppercase vowels
  • Counting y as a vowel (typically not counted)
  • Not initializing the counter to 0
  • Trying to modify the string while iterating
  • Counting special characters or numbers

❓ Frequently Asked Questions

Yes! Convert the string to lowercase first with .lower() to count both uppercase and lowercase vowels.
In basic vowel counting, Y is typically not counted. Advanced versions might include it based on context.
The basic version only counts English vowels (a,e,i,o,u). Advanced versions could use Unicode character properties.
sum(1 for char in text.lower() if char in 'aeiou') is concise and efficient.

🔗 Related Exercises