Count and Display Vowels in a String in Python

I was working on a text-processing project where I needed to quickly count and display vowels in a string.

At first, I thought this would be an easy task. But as I explored, I realized there are multiple ways to achieve this in Python, each with its own advantages.

In this tutorial, I’ll share five simple methods I use to count and display vowels in a string. I’ll also provide full working code examples so you can try them out directly.

Methods to Count and Display Vowels in a String in Python

Let me explain to you the methods to count and display vowels in a string in Python.

Method 1: Use Equality Operator (==) in Python

Python provides this equality operator to check whether both operands are equal. The == operator is the equality operator in Python.

input_string = input('Enter the proverb :')
count = 0
String = input_string.lower()
for vowel in String:
    if vowel == 'a' or vowel == 'e' or vowel == 'i' or vowel == 'o' or vowel == 'u' :
        count+=1
if count == 0:
    print('No vowels found')
else:
    print('Total numbers of vowels in proverb are :' + str(count))
String = input_string.lower()
for vowel in String:
    if vowel == 'a' or vowel == 'e' or vowel == 'i' or vowel == 'o' or vowel == 'u' :

I executed the above example code and added the screenshot below.

Count and Display Vowels in a String in Python

I have used the .lower() built-in Python function to convert the input_string into lowercase, so there will be fewer conditions to apply. Here, Python == operator is used to compare both operands.

Method 2: Use Python’s in operator

Another approach is to count the number of vowels in Python in a string using the in operator. In Python, the in operator determines whether a given value is present in the given sequence (like a string, array, list, or tuple).

Phrase = "Jump for Joy"
count = 0
vowels = ["a", "e", "i", "o", "u"]

for i in range(len(Phrase)):
    if Phrase[i] in vowels:
        count += 1

print("Number of vowels in the given string is: ", count)
for i in range(len(Phrase)):
    if Phrase[i] in vowels:

I executed the above example code and added the screenshot below.

Python Program to Check Vowel

In the above example, I have used the or operator in Python to compare the characters with vowels. We can use the in operator in Python to avoid using these many or operators.

Method 3: Use the count() method in Python

The count() method in Python is used to count the number of specified elements in any iterable, like a string or a list.

NewsHeadline = "Biden Administration Begins Medicare Drug Price Negotiations"

vowels = [NewsHeadline.count(x) for x in "aeiou"]
print('Occurences of vowels aeiou: ', vowels)
print('The numbers vowels in this NewHeadline are: ',sum(vowels))
vowels = [NewsHeadline.count(x) for x in "aeiou"]
print('Occurences of vowels aeiou: ', vowels)
print('The numbers vowels in this NewHeadline are: ',sum(vowels))

Output:

Occurences of vowels aeiou: [3, 6, 9, 3, 1]
The numbers vowels in this NewHeadline are: 22

I executed the above example code and added the screenshot below.

Count the number of each vowel in Python using the count() method

Here, I used the count() method in Python to count the occurrences of each vowel, and the sum() method was used to get the total number of vowels in a given string in Python.

Method 4: Use a Python for loop

Python uses a for loop to iterate over elements in a sequence or a collection. This is the source code of Python to count vowels in a given string using a for loop.

Statement = "Great work comes from loving what you do"

vowels = ["a", "e", "i", "o", "u"]
count = 0
for char in Statement:
    if char in vowels:
        count += 1

print("Number of vowels in the given string is: ", count)
for char in Statement:
    if char in vowels:
        count += 1

Output:

Number of vowels in the given string is: 12

I executed the above example code and added the screenshot below.

Count the Number of Each Vowel in Python

Using a for loop in Python, with the code, we will go through the statement, and the if conditional statement will check if the characters in the given statement are vowels. If it is a vowel, the count will be incremented by +1.

Method 5: Use Python List Comprehension

Python offers List Comprehension, which provides a shorter syntax for creating a new list based on the values of an existing list in a single line.

Quote = "Nothing is impossible, the word itself says ‘I’m possible."
vowel_count = len([char for char in Quote if char in "aeiouAEIOU"])

print("Number of vowels: ", vowel_count)
vowel_count = len([char for char in Quote if char in "aeiouAEIOU"])

Output:

Number of vowels:  16

I executed the above example code and added the screenshot below.

Program to Count the Number of Each Vowel in Python using List Comprehension

I used Python list comprehension to execute for each element and the for loop, which is used to iterate over the list’s elements. It helps to count the number of vowels in a String using one line of code in Python.

Method 6: Use Recursion

In Python, the process in which a function calls itself is called recursion. It helps to ease the method of solving problems by replacing iterative code with recursive statements.

def count_vowels_recursively(string):
    if not string:
        return 0
    else:
        if string[0].lower() in 'aeiou':
            return 1 + count_vowels_recursively(string[1:])
        else:
            return count_vowels_recursively(string[1:])
input_string = "American Airlines Launches Its Longest Non-Stop Flight"
a = count_vowels_recursively(input_string)
print(f"Number of vowels in '{input_string}' is: {a}")

Output:

Number of vowels in 'American Airlines Launches Its Longest Non-Stop Flight' is: 17

I executed the above example code and added the screenshot below.

Python program to check vowels using recursion

Method 7: Use Regular Expressions

Python’s re module provides support for regular expressions, which can be used to solve this problem in a single line of code. Here’s an example:

import re

def count_vowels(string):
    return len(re.findall(r'[aeiou]', string, re.IGNORECASE))

# Example usage
name = "Olivia Thompson"
vowel_count = count_vowels(name)
print(f"The name '{name}' contains {vowel_count} vowels.")

In this solution, we use the re.findall() function to find all occurrences of vowels in the string. The regular expression pattern r'[aeiou]’ matches any vowel character. The re.IGNORECASE flag ensures that both lowercase and uppercase vowels are considered. The len() function is then used to count the number of vowels found.

Conclusion

I hope the above article on Python provides useful information and makes you understand how to count and Display Vowels in a String in Python.

Here, I have covered six methods to count the number of vowels in Python, such as using equality operators (==), using an in operator, using the count() method, using a for loop, using a regular expression, using list comprehension, and using recursion.

You may also like to read other Python articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.