DocsHub
PythonBeginner

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: 3

Logic

  1. Define the vowels — a, e, i, o, u
  2. Go through each character in the string
  3. Check if the character is a vowel
  4. If yes — add 1 to the count
  5. Return the count

Flow

Yes No Yes No Start count = 0 Take each character Is character a vowel? count += 1 Skip it More characters? Return count

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"))       # 0

Code Execution — Solution 1

Trace through count_vowels("hello"):

Stepcharchar in vowels?count
Start0
1st"h"No0
2nd"e"Yes1
3rd"l"No1
4th"l"No1
5th"o"Yes2
Done2

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"))  # 3

Code Execution — Solution 2

Trace through count_vowels("Hello World"):

First s.lower() converts "Hello World""hello world"

Stepcharchar in "aeiou"?count
Start0
1st"h"No0
2nd"e"Yes1
3rd"l"No1
4th"l"No1
5th"o"Yes2
6th" "No2
7th"w"No2
8th"o"Yes3
9th"r"No3
10th"l"No3
11th"d"No3
Done3

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"))  # 3

Code Execution — Solution 3

Trace through count_vowels("programming"):

"programming".lower()"programming" (already lowercase)

The comprehension [char for char in "programming" if char in "aeiou"]:

charchar 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"))  # 3

Code Execution — Solution 4

Trace through count_vowels("hello"):

s = "hello"

vowels.count(vowel)Running total
"a"00
"e"11
"i"01
"o"12
"u"02

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?

SolutionHowBest when
Solution 1Loop + in checkClear and easy to understand
Solution 2lower() + loopCleaner — only 5 vowels to check
Solution 3List comprehensionConcise one-liner
Solution 4count() per vowelReadable and expressive

Output

2
1
3
3
0

On this page