Open In App

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

Last Updated : 24 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string, count the number of vowels present in the given string using Sets. You can use sets to count the number of vowels in a given string in Python. Sets are useful to efficiently check for counting the unique occurrences of vowels.

Input : GeeksforGeeks
Output : No. of vowels : 5
Explaination: The string GeeksforGeeks contains 5 vowels in it 4 letter of 'e' and 1 'o'.

Python Program to Count the Number of Vowels in a String

Below are the methods that we will cover in this article:

  • Using ‘count()’ method
  • Using for Loop
  • Using list comprehension
  • Using ReGex expression

Count the Number of Vowels using Python count()

We can also use the count() method which is an inbuilt Python method in Python for counting the item passed in the parentheses. , So like this, we can also count the vowels in the string by passing the vowels in the parentheses.

Python3




string = "GeekforGeeks!"
vowels = "aeiouAEIOU"
 
count = sum(string.count(vowel) for vowel in vowels)
print(count)


Output:

5

Time Complexity: O(n*m), where n is the length of the string and m is the number of vowels.
Auxiliary Space: O(1)

Count Vowels in String Using for Loop

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 the set vowel. 3. If it is present, the vowel count is incremented. Below is the implementation of the above approach: 

Python3




# 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

Time Complexity: O(n), where n is the length of the string. We traverse the string character by character and for each character, we do a constant time operation to check if it is a vowel or not.
Auxiliary Space: O(1). We only used constant extra space for setting up the set.

Count the Number of Vowels in the given string using List Comprehension

We create a string containing all the vowels then use list comprehension to iterate over each character in the given string and check if it is a vowel or not if the character is a vowel, add it to a list. Use the len() function to count the number of elements in the list, which will give the count of vowels in the given string.

Print the count of vowels in the given string.

Python3




def vowel_count(str):
    # Creating a list of vowels
    vowels = "aeiouAEIOU"
     
    # Using list comprehension to count the number of vowels in the string
    count = len([char for char in str if char in vowels])
     
    # Printing the count of vowels in the string
    print("No. of vowels :", count)
 
# Driver code
str = "GeeksforGeeks"
 
# Function Call
vowel_count(str)


Output

No. of vowels : 5

Time Complexity: O(n), where n is the length of the string. 
Auxiliary Space: O(1). 

Count Vowels in String Using ReGex expression

To count the number of vowels in a string using regular expressions (regex), you can utilize the re module in Python. we use the re.findall() function to find all occurrences of vowels in the string based on the specified regex pattern

Python3




import re
 
string = "GeeksforGeeks"
vowels = r'[aeiouAEIOU]'
count = len(re.findall(vowels, string))
print(count)


Output:

5

Time Complexity: O(n), where n is the length of the string. 
Auxiliary Space: O(1). 



Similar Reads

Python Program to Find Duplicate sets in list of sets
Given a list of sets, the task is to write a Python program to find duplicate sets. Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result. Input : test_list = [{4, 5, 6, 9}, {6, 4, 1, 5}, {
8 min read
Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
Given four integers X, Y, M and N. The task is to find the number of ways to form a word by choosing X number of vowels and Y number of consonants from total numbers of M vowels and N consonants.Examples: Input : X = 2, Y = 2, M = 3, N = 3 Output : 216 The total number of ways of choosing 2 vowels from a total number of 3 vowels is [Tex]3\choose2 [
6 min read
Distribute given arrays into K sets such that total sum of maximum and minimum elements of all sets is maximum
Given two arrays, the first arr[] of size N and the second brr[] of size K. The task is to divide the first array arr[] into K sets such that the i-th set should contain brr[i] elements from the second array brr[], and the total sum of maximum and minimum elements of all sets is maximum. Examples: Input: n = 4, k = 2, arr[] = {10, 10, 11, 11 }, brr
8 min read
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 design a simple approach that we will be following in
10 min read
Python Program to Count Vowels in a String Using Recursion
Given a string, our task is to count the number of vowels in the string using recursion in Python and return the number of vowels. Examples: Input: acbedOutput: 2Input: geeksforgeeksOutput: 5Explanation: We are counting vowels in a string and printing the number of vowels.Python Program to Count Vowels in a String Using RecursionWe can use a recurs
3 min read
C program to count number of vowels and consonants in a String
Given a string and write a C program to count the number of vowels and consonants in this string. Examples: Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21Using For LoopsTake the string as inputTake each character from this string to checkIf this character is
4 min read
Count the number of vowels occurring in all the substrings of given string
Given a string of length N of lowercase characters containing 0 or more vowels, the task is to find the count of vowels that occurred in all the substrings of the given string. Examples: Input: str = "abc" Output: 3The given string "abc" contains only one vowel = 'a' Substrings of "abc" are = {"a", "b", "c", "ab", "bc, "abc"} Hence, the sum of occu
12 min read
Count the number of vowels occurring in all the substrings of given string | Set 2
Given a string str[] of length N of lowercase characters containing 0 or more vowels, the task is to find the count of vowels that occurred in all the substrings of the given string. Examples: Input: str = “abc” Output: 3The given string “abc” contains only one vowel = ‘a’ Substrings of “abc” are = {“a”, “b”, “c”, “ab”, “bc, “abc”} Hence, the sum o
5 min read
Number of words that can be made using exactly P consonants and Q vowels from the given string
Given a string str and two integers P and Q. The task is to find the total count of words that can be formed by choosing exactly P consonants and Q vowels from the given string.Examples: Input: str = "geek", P = 1, Q = 1 Output: 8 "ge", "ge", "eg", "ek", "eg", "ek", "ke" and "ke" are the possible words.Input: str = "crackathon", P = 4, Q = 3 Output
7 min read
Program to count vowels, consonant, digits and special characters in string.
Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.Examples: Input : str = "geeks for geeks121" Output : Vowels: 5 Consonant: 8 Digit: 3 Special Character: 2 Input : str = " A1 B@ d adc" Output : Vowels: 2 Consonant: 4 Digit: 1 Special Character: 6 C/C++
6 min read
three90RightbarBannerImg