Count Vowels in String in Python

Count the number of vowels in a given string.

BeginnerString ProgramsExample 3 of 25
count-vowels-in-string.py
1# Program to count vowels in a string
2
3s = input("Enter a string: ")
4
5vowels = "aeiouAEIOU"
6count = sum(1 for ch in s if ch in vowels)
7
8print("Number of vowels:", count)

Output

Enter a string: hello world
Number of vowels: 3

What's going on

We use a comprehension and sum to count characters that appear in the vowel set.

After this program

If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.