Hello everyone, welcome back toprogramminginpython.com! Here in this tutorial am going to tell how you can count vowels in a string when an input string is received you will be able to count the number of vowels present in that string.
Master the basics of data analysis in Python. Expand your skillset by learning scientific computing with numpy.
Take the course on Introduction to Python onDataCamp herehttps://bit.ly/datacamp-intro-to-python
You can also watch the video on YouTubehere.
Task
Python program to count the number of each vowel in a given string.
Approach
- Read an input string using
input()
orraw_input()
. - Declare vowels as a string `vowels = ‘aeiou’`
- Make the input string case insensitive using `casefold()` method
- Create a dictionary with each vowel a key and value 0
- Loop the string and check for vowels and if found increment the value in the dictionary
- Print the result(dictionary)
Program
__author__ = 'Avinash'# Python3 program to count vowels in a string# string of vowelsvowels = 'aeiou'input_str = input("Enter a string: ")# make input string case insensitiveinput_str = input_str.casefold()# make a dictionary with each vowel a key and value 0vowels_count = {}.fromkeys(vowels, 0)# count the number of each vowelsfor letter in input_str: if letter in vowels_count: vowels_count[letter] += 1print(vowels_count)
Output

Count Vowels in a String – Code Visualization
Course Suggestion
Machine Learning is everywhere! So I strongly suggest you to take the course below.
Course:Machine Learning Adv: Support Vector Machines (SVM) Python
Feel free to check other Python programs likesorting and searching algorithms orpattern programming and many more programshere.