Beginner Python Exercises

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()).

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Lowercasing the text
  • Testing membership with in
  • Counting with sum() over booleans
"Code Utility"
c
o
d
e
u
t
i
l
i
t
y
5 vowels (the space is skipped).

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.

From a tally sheet to code
for character in text👉 Move along the text one letter at a time
character.lower()🔡 Treat capital and small letters the same
in "aeiou"🔍 Is this letter a vowel?
sum(...)✔️ Count the ticks: 5
Check every character, count the ones that match.
In one sentence

Counting vowels means going through a text and counting the characters that are a, e, i, o or u, regardless of case.

Key points

  1. character.lower() makes "U" and "u" the same, so one check covers both cases.
  2. character.lower() in "aeiou" is True for a vowel and False otherwise.
  3. sum(...) counts True as 1 and False as 0, so it returns the number of vowels.
  4. "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 True and False can 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

Run code →
main.py
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}")
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Forgetting uppercase letters

"A" is not in "aeiou", so capital vowels are not counted.

Problem
sum(character in "aeiou" for character in text)
Fix
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.

All challenges