Count Vowels in a String in Python
Count a, e, i, o and u in any sentence with one line: sum(ch in "aeiou" for ch in text.lower()).
- Lowercasing the text
- Testing membership with in
- Counting with sum() over booleans
Check every character
The problem
Counting certain characters is a common text task: vowels in a word game, digits in a password, punctuation in a sentence. You need to check every character and keep a count, while treating uppercase and lowercase letters the same.
Think of it like this
Counting vowels is like moving a finger along a word, letter by letter, with a tally sheet in the other hand. Each time the letter is a, e, i, o or u, capital or small, you make one tick. At the end, the ticks are the count.
for character in text👉 Move along the text one letter at a timecharacter.lower()🔡 Treat capital and small letters the samein "aeiou"🔍 Is this letter a vowel?sum(...)✔️ Count the ticks: 5Counting vowels means going through a text and counting the characters that are a, e, i, o or u, regardless of case.
Key points
character.lower()makes"U"and"u"the same, so one check covers both cases.character.lower() in "aeiou"is True for a vowel and False otherwise.sum(...)countsTrueas 1 andFalseas 0, so it returns the number of vowels."Code Utility"has 5 vowels: o, e, U, i, i.
When should you use it?
- Counting characters of a kind: vowels, digits, spaces, punctuation.
- Simple text statistics and word games.
- Learning that
TrueandFalsecan be added like numbers.
When not to use it
- Languages with accented vowels such as é or ơ: extend the set of vowels or use Unicode tools.
- Counting every character, not just one group: use
Counter.
Example code
text = "Code Utility"
# Count characters that belong to the vowel set.
count = sum(character.lower() in "aeiou" for character in text)
print(f"vowels={count}")
Result of running the code
vowels=5
Code explanation
The generator expression looks at each character of text once. character.lower() in "aeiou" turns the character into lowercase and checks whether it is one of the five vowels, giving True or False. sum() treats True as 1 and False as 0, so adding all those results gives the number of vowels.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Forgetting uppercase letters
"A" is not in "aeiou", so capital vowels are not counted.
sum(character in "aeiou" for character in text)
sum(character.lower() in "aeiou" for character in text)
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Count Vowels Count the vowels in a piece of text. Easy
- Count Uppercase Letters Count the capital letters in a text. Easy