Count Vowels in String
Count the number of vowels in a given string.
BeginnerTopic: String Programs
Python Count Vowels in String Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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
Understanding Count Vowels in String
We use a comprehension and sum to count characters that appear in the vowel set.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.