Counting vowels in a string is a popular exercise for learning loops, conditions and strings. The vowels are a, e, i, o and u, and every other letter is a consonant. In this lesson you will count vowels, then count vowels and consonants together, and finally count each vowel separately.
Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.
The logic
- Convert the text to lowercase, so that ‘A’ and ‘a’ are treated the same.
- Go through the text one character at a time.
- If the character is in ‘aeiou’, count it as a vowel.
Program 1: count the vowels
def count_vowels(text):
count = 0
for ch in text.lower():
if ch in 'aeiou':
count += 1
return count
print(count_vowels('Hello World'))
print(count_vowels('Python Programming'))
print(count_vowels('rhythm'))
print(count_vowels('AEIOU'))Output:
3
4
0
5In ‘Hello World’, the vowels are e, o and o, so the count is 3. The word ‘rhythm’ has no vowels at all. text.lower() makes ‘AEIOU’ work too.
A shorter version
def count_vowels(text):
return sum(1 for ch in text.lower() if ch in 'aeiou')
print(count_vowels('Hello World'))Output:
3The expression creates a 1 for every vowel, and sum() adds them up.
Program 2: count vowels and consonants
A consonant is a letter that is not a vowel. We use isalpha() to skip spaces, digits and punctuation.
text = 'Hello World'
vowels = sum(1 for ch in text.lower() if ch in 'aeiou')
consonants = sum(1 for ch in text.lower() if ch.isalpha() and ch not in 'aeiou')
print('Vowels:', vowels)
print('Consonants:', consonants)Output:
Vowels: 3
Consonants: 7The text has 11 characters, but one is a space, so there are 10 letters: 3 vowels and 7 consonants.
Program 3: count each vowel separately
A dictionary can keep a separate count for each vowel.
counts = {}
for ch in 'education':
if ch in 'aeiou':
counts[ch] = counts.get(ch, 0) + 1
print(counts)Output:
{'e': 1, 'u': 1, 'a': 1, 'i': 1, 'o': 1}The collections module has a ready-made tool for counting:
from collections import Counter
print(Counter(ch for ch in 'education' if ch in 'aeiou'))Output:
Counter({'e': 1, 'u': 1, 'a': 1, 'i': 1, 'o': 1})To learn more, see our lesson on Python dictionaries.
Take the text from the user (on your computer)
text = input('Enter a sentence: ')
print('Vowels:', count_vowels(text))Our online compiler does not support input() yet, so use a fixed value there.
Common mistakes
- Forgetting capital letters. Without lower(), a check for ‘aeiou’ misses ‘A’, ‘E’ and so on.
- Counting spaces and digits as consonants. Use isalpha() to count only letters.
- Checking each vowel separately with many if statements. The in operator on the string ‘aeiou’ is shorter and clearer.
Practice questions
- Count the vowels in ‘Programming Language’.
- Print the vowels found in a word, in the order they appear.
- Count the uppercase and lowercase letters in a sentence.
- Remove all vowels from a string.
- Find the word with the most vowels in a sentence.
FAQs
Is ‘y’ a vowel?
In this program, no. Only a, e, i, o and u are counted as vowels. In English, ‘y’ can sound like a vowel in words such as ‘rhythm’, but programs usually treat it as a consonant.
How do I make the count case-insensitive?
Convert the text with lower() (or upper()) before you check each character.
How do I count the vowels in a sentence with spaces?
The same program works, because spaces are not in ‘aeiou’ and are simply skipped.
