Count Vowels in String

Count the number of vowels in a given string.

PythonBeginner
Python
# Program to count vowels in a string

s = input("Enter a string: ")

vowels = "aeiouAEIOU"
count = sum(1 for ch in s if ch in vowels)

print("Number of vowels:", count)

Output

Enter a string: hello world
Number of vowels: 3

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