Count Vowels
Learn how to count the number of vowels in a string in Python.
Count Vowels
Problem
Given a string, count how many vowels it contains. Vowels are a, e, i, o, u — both uppercase and lowercase.
Input: "hello"
Output: 2
Input: "Python"
Output: 1
Input: "programming"
Output: 3
Input: "Hello World"
Output: 3Logic
- Define the vowels —
a, e, i, o, u - Go through each character in the string
- Check if the character is a vowel
- If yes — add 1 to the count
- Return the count
Flow
Solution 1 — using a loop
Loop through every character and check if it is in the vowels string.
def count_vowels(s):
# define all vowels — lowercase and uppercase
vowels = "aeiouAEIOU"
# start count at zero
count = 0
# check each character one by one
for char in s:
if char in vowels: # is this character a vowel?
count += 1 # yes — increment the count
return count
print(count_vowels("hello")) # 2
print(count_vowels("Python")) # 1
print(count_vowels("programming")) # 3
print(count_vowels("Hello World")) # 3
print(count_vowels("rhythm")) # 0Code Execution — Solution 1
Trace through count_vowels("hello"):
| Step | char | char in vowels? | count |
|---|---|---|---|
| Start | — | — | 0 |
| 1st | "h" | No | 0 |
| 2nd | "e" | Yes | 1 |
| 3rd | "l" | No | 1 |
| 4th | "l" | No | 1 |
| 5th | "o" | Yes | 2 |
| Done | 2 |
Solution 2 — using lower() to simplify
Instead of checking both uppercase and lowercase vowels, convert the string to lowercase first. Then only check against lowercase vowels.
def count_vowels(s):
# convert to lowercase — now we only need to check 5 vowels
s = s.lower()
vowels = "aeiou"
count = 0
for char in s:
if char in vowels:
count += 1
return count
print(count_vowels("Hello World")) # 3
print(count_vowels("PYTHON")) # 1
print(count_vowels("Programming")) # 3Code Execution — Solution 2
Trace through count_vowels("Hello World"):
First s.lower() converts "Hello World" → "hello world"
| Step | char | char in "aeiou"? | count |
|---|---|---|---|
| Start | — | — | 0 |
| 1st | "h" | No | 0 |
| 2nd | "e" | Yes | 1 |
| 3rd | "l" | No | 1 |
| 4th | "l" | No | 1 |
| 5th | "o" | Yes | 2 |
| 6th | " " | No | 2 |
| 7th | "w" | No | 2 |
| 8th | "o" | Yes | 3 |
| 9th | "r" | No | 3 |
| 10th | "l" | No | 3 |
| 11th | "d" | No | 3 |
| Done | 3 |
Solution 3 — using a list comprehension
Build a list of only the vowel characters, then count how many are in it.
def count_vowels(s):
vowels = "aeiou"
# build a list of every character that is a vowel
# then count how many items are in that list
return len([char for char in s.lower() if char in vowels])
print(count_vowels("hello")) # 2
print(count_vowels("Python")) # 1
print(count_vowels("programming")) # 3
print(count_vowels("Hello World")) # 3Code Execution — Solution 3
Trace through count_vowels("programming"):
"programming".lower() → "programming" (already lowercase)
The comprehension [char for char in "programming" if char in "aeiou"]:
char | char in "aeiou"? | Added to list? |
|---|---|---|
"p" | No | — |
"r" | No | — |
"o" | Yes | ["o"] |
"g" | No | — |
"r" | No | — |
"a" | Yes | ["o", "a"] |
"m" | No | — |
"m" | No | — |
"i" | Yes | ["o", "a", "i"] |
"n" | No | — |
"g" | No | — |
Result list: ["o", "a", "i"]
len(["o", "a", "i"]) = 3
Solution 4 — using count() method
Use the string count() method to count each vowel separately and add them all up.
def count_vowels(s):
# convert to lowercase once
s = s.lower()
# count each vowel individually and sum them all
return sum(s.count(vowel) for vowel in "aeiou")
print(count_vowels("hello")) # 2
print(count_vowels("Python")) # 1
print(count_vowels("programming")) # 3
print(count_vowels("Hello World")) # 3Code Execution — Solution 4
Trace through count_vowels("hello"):
s = "hello"
vowel | s.count(vowel) | Running total |
|---|---|---|
"a" | 0 | 0 |
"e" | 1 | 1 |
"i" | 0 | 1 |
"o" | 1 | 2 |
"u" | 0 | 2 |
sum([0, 1, 0, 1, 0]) = 2
s.count(x) counts how many times x appears in s. It is clean and readable — but it scans the whole string once per vowel. For short strings this is fine. For very long strings, Solution 1 or 2 is more efficient since they scan the string only once.
Which solution to use?
| Solution | How | Best when |
|---|---|---|
| Solution 1 | Loop + in check | Clear and easy to understand |
| Solution 2 | lower() + loop | Cleaner — only 5 vowels to check |
| Solution 3 | List comprehension | Concise one-liner |
| Solution 4 | count() per vowel | Readable and expressive |
Output
2
1
3
3
0