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

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 : GeeksforGeeksOutput : No. of vowels : 5Explaination: 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

• 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).

Previous
Next