## Python Program to Count Number of Vowels in a String

Last Updated: 01-Aug-2024 16:41:05
109.2K Views

Counting the number of vowels in a string is a common task that can be solved in multiple ways using Python. This article will cover various methods to achieve this, from basic loops to more advanced techniques using builtin functions and list comprehensions.

### Method 1: Using a Simple Loop

The most straightforward way to count vowels in a string is by iterating through each character and checking if it is a vowel.

def count_vowels_simple(string):
vowels = 'aeiouAEIOU'
count = 0
for char in string:
if char in vowels:
count += 1
return count

# Example usage
string = "Hello, World!"
print(count_vowels_simple(string))


Explanation:

• We define a string vowels containing all vowel characters (both uppercase and lowercase).
• We initialize a counter count to zero.
• We loop through each character in the input string.
• If the character is found in the vowels string, we increment the counter.
• Finally, we return the count.

### Method 2: Using List Comprehension

List comprehension provides a more concise way to achieve the same result.

def count_vowels_list_comprehension(string):
vowels = 'aeiouAEIOU'
return sum(1 for char in string if char in vowels)

# Example usage
string = "Hello, World!"
print(count_vowels_list_comprehension(string))


Explanation:

• The list comprehension [char for char in string if char in vowels] generates a list of vowels found in the string.
• The sum function adds up 1 for each vowel in this list, giving us the total count.

### Method 3: Using Regular Expressions

Regular expressions provide a powerful way to search for patterns in text.

import re

def count_vowels_regex(string):
pattern = r'[aeiouAEIOU]'
return len(re.findall(pattern, string))

# Example usage
string = "Hello, World!"
print(count_vowels_regex(string))


Explanation:

• We define a regular expression pattern [aeiouAEIOU] to match any vowel.
• The re.findall function returns a list of all matches in the string.
• We use len to count the number of matches.

### Method 4: Using the filter Function

The filter function can be used to filter out only the vowels from the string.

def count_vowels_filter(string):
vowels = 'aeiouAEIOU'
return len(list(filter(lambda char: char in vowels, string)))

# Example usage
string = "Hello, World!"
print(count_vowels_filter(string))  # Output: 3


Explanation:

• The filter function applies a lambda function to each character in the string, keeping only those that are vowels.
• We convert the filter object to a list and use len to count the number of vowels.

### Method 5: Using collections.Counter

The collections.Counter class provides a way to count the occurrences of each character in the string.

from collections import Counter

def count_vowels_counter(string):
vowels = 'aeiouAEIOU'
counter = Counter(string)
return sum(counter[char] for char in vowels)

# Example usage
string = "Hello, World!"
print(count_vowels_counter(string))  # Output: 3


Explanation:

• We create a Counter object from the string, which counts the occurrences of each character.
• We sum the counts of all vowel characters using a generator expression.

### Method 6: Using str.count for Each Vowel

We can use the count method of strings to count each vowel individually and sum the results and is most simple way to count the number of vowels in a string using Python.

def count_vowels_str_count(string):
vowels = 'aeiouAEIOU'
return sum(string.count(vowel) for vowel in vowels)

# Example usage
string = "Hello, World!"
print(count_vowels_str_count(string))  # Output: 3


Explanation:

• For each vowel in the vowels string, we use the count method to get its occurrences in the input string.
• We sum these counts to get the total number of vowels.

### Conclusion

In this article, we've explored several methods to count the number of vowels in a string using Python. Each method has its own advantages and can be chosen based on the specific requirements and constraints of your task:

1. Simple Loop: Easy to understand and implement.
2. List Comprehension: Concise and Pythonic.
3. Regular Expressions: Powerful for complex patterns.
4. Filter Function: Functional programming approach.
5. collections.Counter: Efficient for counting all characters.
6. str.count for Each Vowel: Direct and straightforward.

Choose the method that best fits your needs and coding style.

##### Prathmesh Yelne Follow
CEO & Founder of Codegyan