Count Number of Vowels using Sets in Given String - Python Last Updated : 03 Mar, 2025 Summarize Comments Improve Suggest changes Share 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. Count number of vowels using sets in given string Comment More infoAdvertise with us Next Article Python Set - Pairs of Complete Strings in Two Sets S Sharad_Bhardwaj Follow Improve Article Tags : Python python-set Python string-programs Python set-programs Practice Tags : pythonpython-set Similar Reads Count the number of Unique Characters in a String in Python We are given a string, and our task is to find the number of unique characters in it. For example, if the string is "hello world", the unique characters are {h, e, l, o, w, r, d}, so the output should be 8.Using setSet in Python is an unordered collection of unique elements automatically removing du 2 min read Python - Count and display vowels in a string Given a string, we need to write a Python program that counts and displays all the vowels present in the string. Let's explore different ways of counting and displaying vowels in a string.We need to identify all the vowels in the string both uppercase and lowercase.We should display each vowel found 3 min read Python Set - Pairs of Complete Strings in Two Sets The task of finding pairs of complete strings in two sets in Python involves identifying string pairs from two different lists such that, when combined, they contain all the letters of the English alphabet. For example, given two sets a = ['abcdefgh', 'geeksforgeeks', 'lmnopqrst', 'abc'] and b = ['i 3 min read Count of substrings consisting only of vowels Given a string S, the task is to count all substrings which contain only vowels. Examples: Input: S = "geeksforgeeks" Output: 7 Explanation: Substrings {"e", "ee", "e", "o", "e", "ee", "e"} consists only of vowels. Input: S = "aecui" Output: 6 Explanation: Substrings {"a", "ae", "e", "u", "ui", "i"} 11 min read Python | Count the Number of matching characters in a pair of string The problem is about finding how many characters are the same in two strings. We compare the strings and count the common characters between them. In this article, we'll look at different ways to solve this problem.Using Set Sets are collections of unique items, so by converting both strings into se 2 min read Python Set | Check whether a given string is Heterogram or not Given a string S of lowercase characters. The task is to check whether a given string is a Heterogram or not using Python. A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. Input1: S = "the big dwarf only jumps"Output1: YesExplanation: Each alphabe 3 min read Like