Python program to count number of vowels using sets in given string
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): # Intializing 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
Recommended Posts:
- Program to count vowels in a string (Iterative and Recursive)
- Python | Count and display vowels in a string
- Program to count vowels, consonant, digits and special characters in string.
- Count the number of vowels occurring in all the substrings of given string
- Count the pairs of vowels in the given string
- Program to duplicate Vowels in String
- Program to remove vowels from a String
- Python | Program to accept the strings which contains all vowels
- Python program to find common elements in three lists using sets
- Python | Ways to count number of substring in string
- Encrypt string with product of number of vowels and consonants in substring of size k
- Python | Count the Number of matching characters in a pair of string
- Python Program to Count number of binary strings without consecutive 1's
- Python Program to Count trailing zeroes in factorial of a number
- Check if a string can be converted to another string by replacing vowels and consonants
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



