Python Program to Count Vowels in a String

Short answer
To count vowels in Python, loop over the lowercase string and count the characters found in "aeiou": sum(1 for ch in text.lower() if ch in "aeiou").

Counting vowels is a good exercise in looping over a string, testing membership with in and keeping a running total. It also appears in real text tasks such as simple readability checks or word games.

The key decision is what counts as a vowel. This page uses a, e, i, o, u in either case and does not treat y as a vowel, which is the common textbook rule. Changing the vowel string changes the rule.

Quick facts
InputA string such as "Programming"
OutputAn integer: how many vowels it contains
ApproachLowercase the text and test each character against "aeiou"
Time complexityO(n) for a string of n characters

Approach to count vowels step by step

  1. Store the vowels in a string: vowels = "aeiou".
  2. Convert the input to lowercase with lower() so A and a are handled by one rule.
  3. Walk through every character with a for loop.
  4. If ch in vowels, add one to a counter.
  5. Print or return the counter.

Because each character is a one-letter string, ch in "aeiou" is a quick membership test.

How the vowel counting code works

The loop version keeps a variable count that starts at 0. Every time the if test succeeds, count += 1 runs. After the loop, count holds the answer.

The compact version uses a generator expression inside sum(). It yields a 1 for each vowel, and sum() adds them up. Both do the same work; use the loop when you are learning and the one-liner when you already read it fluently.

Python
text = "Education"
count = 0
for ch in text.lower():
    if ch in "aeiou":
        count += 1
print(count)

Other ways to count vowels in Python

You can also add text.lower().count(v) for each vowel with str.count(), which is handy when you want the number for each vowel separately. For a full report, a dictionary comprehension builds the per-vowel counts in one line. If you want counts for every letter, see the character frequency program.

MethodCode ideaGives
Loop with counterif ch in "aeiou": count += 1Total
sum() with generatorsum(1 for ch in s if ch in "aeiou")Total
str.count() per vowel{v: s.count(v) for v in "aeiou"}Count of each vowel

Examples you can run

Press Run, then edit the code and run it again.

Count vowels with a loop

The classic beginner version with a counter variable.

Count vowels with a loop
def count_vowels(text):
    count = 0
    for ch in text.lower():
        if ch in "aeiou":
            count += 1
    return count

print(count_vowels("Programming"))
print(count_vowels("SKY"))
print(count_vowels("Education"))
Expected output3
0
5

One-line version and counts per vowel

sum() gives the total and a dictionary comprehension shows each vowel.

One-line version and counts per vowel
text = "Python is a Beautiful language"
lower = text.lower()
print(sum(1 for ch in lower if ch in "aeiou"))
print({v: lower.count(v) for v in "aeiou"})
Expected output12
{'a': 4, 'e': 2, 'i': 2, 'o': 1, 'u': 3}

Edge case: empty text and no vowels

An empty string gives 0 and so does a word with only consonants.

Edge case: empty text and no vowels
def count_vowels(text):
    return sum(1 for ch in text.lower() if ch in "aeiou")

print(count_vowels(""))
print(count_vowels("rhythm"))
print(count_vowels("AEIOU aeiou"))
Expected output0
0
10

Ask the user for a sentence

Read text with input() and report the vowel and consonant counts.

Ask the user for a sentence
sentence = input("Type a sentence: ")
vowels = sum(1 for ch in sentence.lower() if ch in "aeiou")
letters = sum(1 for ch in sentence if ch.isalpha())
print("Vowels:", vowels)
print("Consonants:", letters - vowels)
Expected outputType a sentence: Learning Python is fun
Vowels: 6
Consonants: 13

Common mistakes

Writing the condition with or

✗ Wrong

ch = "z"
if ch == "a" or "e" or "i":
    print("vowel")
Prints "vowel" for every character, because the non-empty string "e" is always truthy

✓ Right

ch = "z"
if ch in "aeiou":
    print("vowel")
else:
    print("not a vowel")

Compare against a collection of vowels with in, not a chain of or with bare strings.

Forgetting uppercase letters

✗ Wrong

text = "APPLE"
count = sum(1 for ch in text if ch in "aeiou")
print(count)
0 (uppercase vowels are not in "aeiou")

✓ Right

text = "APPLE"
count = sum(1 for ch in text.lower() if ch in "aeiou")
print(count)

Call text.lower() first, or include the uppercase vowels in the vowel string.

Python Program to Count Vowels in a String: questions people ask

How do I count vowels in a string in Python?

Loop through the lowercase string and count characters that are in "aeiou", or use sum(1 for ch in s.lower() if ch in "aeiou").

Is y a vowel in this program?

No. The program counts only a, e, i, o and u. If your rule includes y, add it to the vowel string, but be aware that "y" is a consonant in words such as "yes".

How do I count each vowel separately?

Use a dictionary comprehension such as {v: s.lower().count(v) for v in "aeiou"}. It returns a count for every vowel, including zeros.

Keep learning

Step-by-step lessons:
Python StringsPython for LoopsPython String Methods

Related:
Python Program to Count Character Frequency in a StringPython String count() MethodPython Program to Check Palindrome (String and Number)Python Word Count Program: Count Words in a String

More in string programs:
Python Program to Check Palindrome (String and Number)Python Anagram Program: Check if Two Strings Are AnagramsPython Word Count Program: Count Words in a StringPython Program to Count Character Frequency in a String See all →