Python Program to Count the Number of Each Vowel

Posted in

Python Program to Count the Number of Each Vowel
vinaykhatri

Vinay Khatri
Last updated on December 24, 2024

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

    Prerequisite Python Program to Count the Number of Each Vowel

    • 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.

    Python 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 the count() method, which can be used to count the number of occurrences of a character in a string.

    Python 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:

    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:

    Leave a Comment on this Post

    0 Comments