Python Program to Count the Number of Each Vowel

By | October 3, 2021

Here in this article, we have provided two python source codes that count the number of vowels present in the user entered string.

Prerequisite Topics to Create this Program

  • Python Loop
  • Python Input-Output
  • Python string
  • Python member operators
  • Python string methods
  • Python string count method
  • Python Dictionary

Python Program to Count the Number of Each Vowel (Using For Loop)

Steps:

  • Ask the user to enter a string.
  • Use the for loop and iterate through the user entered string character by character.
  • check for the condition if the character is a vowel, then using a dictionary makes a key-value pair where the key would be the vowel itself and the value is its occurrence.
  • At last print the dictionary.

Code:

string = input("Enter the String: ").lower()

vowel = "aeiou"

count ={'a':0, 'e':0,'i':0,'o':0,'u':0}   #dictionary whihc keys are vowels and its values arevowel occurance
#iterate through string
for i in string:
    #check if i is a vowel
    if i in vowel:
        if  not(count[i] == 0):
            count[i] +=1    #increse the count of vowel by 1 if it occure again
        else:
            count[i] =1   #set the count of vowel 1 if the vowel occuring first time.

for j in count:
    print("There are ", count[j], j," in", string )

Output 1:

Enter the String: This is an example
There are 2 a in this is an example
There are 2 e in this is an example
There are 2 i in this is an example
There are 0 o in this is an example
There are 0 u in this is an example

Output 2:

Enter the String: Welcome to techgeekbuzz
There are 0 a in welcome to techgeekbuzz
There are 5 e in welcome to techgeekbuzz
There are 0 i in welcome to techgeekbuzz
There are 2 o in welcome to techgeekbuzz
There are 1 u in welcome to techgeekbuzz

Python Program to Count the Number of Each Vowel (Using String Count method):

In python, we have count() method, which can be used to count the number of occurrence of a character in a string.

Vamware

Code:

string = input("Enter the String: ").lower()

vowel = "aeiou"
count ={ } # create an empty dictionary

for i in vowel:
    #count the number of occurance of vowles
    count[i]= string.count(i)

for j in count:
    print("There are ", count[j], j," in", string )
    

Output 1:

Vamware
Enter the String: This is an example
There are 2 a in this is an example
There are 2 e in this is an example
There are 2 i in this is an example
There are 0 o in this is an example
There are 0 u in this is an example

Output 2:

Enter the String: Welcome to techgeekbuzz
There are 0 a in welcome to techgeekbuzz
There are 5 e in welcome to techgeekbuzz
There are 0 i in welcome to techgeekbuzz
There are 2 o in welcome to techgeekbuzz
There are 1 u in welcome to techgeekbuzz

People are also reading:

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.