Skip to content
Related Articles

Related Articles

Improve Article

Python program to count number of vowels using sets in given string

  • Last Updated : 18 Dec, 2019

Given a string, count the number of vowels present in given string using Sets.

Prerequisite: Sets in Python

Examples:

Input : GeeksforGeeks
Output : No. of vowels : 5

Input : Hello World
Output : No. of vowels :  3

Approach:
1. Create a set of vowels using set() and initialize a count variable to 0.
2. Traverse through the alphabets in the string and check if the letter in the string is present in set vowel.
3. If it is present, the vowel count is incremented.

Below is the implementation of above approach:






# Python3 code to count vowel in 
# a string using set
  
# Function to count vowel
def vowel_count(str):
      
    # Initializing count variable to 0
    count = 0
      
    # Creating a set of vowels
    vowel = set("aeiouAEIOU")
      
    # Loop to traverse the alphabet
    # in the given string
    for alphabet in str:
      
        # If alphabet is present
        # in set vowel
        if alphabet in vowel:
            count = count + 1
      
    print("No. of vowels :", count)
      
# Driver code 
str = "GeeksforGeeks"
  
# Function Call
vowel_count(str)

Output:

No. of vowels : 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :