Count Number of Vowels using Sets in Given String - Python Last Updated : 03 Mar, 2025 Comments Improve Suggest changes Like Article Like Report We are given a string and our task is to count the number of vowels present in it. Vowels in English include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). Using sets, we can efficiently check for vowel presence due to their fast membership lookup. For example, if the input string is "Beautiful Day" then the output should be 6 as the vowels present are 'e', 'a', 'u', 'i', 'a', and 'y'.Using a Set for Fast LookupA set allows quick membership testing, making it an efficient way to count vowels. We iterate through the string and check if each character is in the set of vowels. Python s = "Python Programming" vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} c = sum(1 for ch in s if ch in vowels) print("Number of vowels:", c) OutputNumber of vowels: 4 Explanation:We define a set of vowels.We iterate over the string and count characters that are present in the vowel set.sum(1 for char in s if char in vowels) ensures we get the total vowel count.Using a Set and IntersectionInstead of iterating through the string, we use set intersection to find common elements between the string and the vowel set. Python s = "Set operations in Python" vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} c = sum(s.count(v) for v in set(s) & vowels) print("Number of vowels:", c) OutputNumber of vowels: 8 Explanation:set(s) & vowels extracts unique vowels from the string.sum(s.count(v) for v in set(s) & vowels) counts occurrences of each vowel in the string. Comment More infoAdvertise with us Next Article Count Number of Vowels using Sets in Given String - Python S Sharad_Bhardwaj Follow Improve Article Tags : Misc Strings Python Python Programs DSA python-set Python string-programs Python set-programs +4 More Practice Tags : Miscpythonpython-setStrings Similar Reads Python Program to Count the Number of Vowels in a String In this article, we will be focusing on how to print each word of a sentence along with the number of vowels in each word using Python. Vowels in the English language are: 'a', 'e', 'i', 'o', 'u'. So our task is to calculate how many vowels are present in each word of a sentence. So let us first des 10 min read Count the number of characters in a String - Python The goal here is to count the number of characters in a string, which involves determining the total length of the string. For example, given a string like "GeeksForGeeks", we want to calculate how many characters it contains. Letâs explore different approaches to accomplish this.Using len()len() is 2 min read Count Occurance of Substring in a List of Strings - Python To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.Using sum() and Generator ExpressionThis method uses a generator 2 min read Recursively Count Vowels From a String in Python Python is a versatile and powerful programming language that provides various methods to manipulate strings. Counting the number of vowels in a string is a common task in text processing. In this article, we will explore how to count vowels from a string in Python using a recursive method. Recursion 3 min read Python program to check if given string is vowel Palindrome Given a string (may contain both vowel and consonant letters), remove all consonants, then check if the resulting string is palindrome or not. Examples: Input : abcuhuvmnba Output : YES Explanation : The consonants in the string "abcuhuvmnba" are removed. Now the string becomes "auua". Input : xayzu 5 min read Like