How to Count and Display Vowels in a String in Python? [6 different ways]

Vowels are an essential part of the English language, and counting the number of vowels in a string can be useful in various programming applications. In this article, I will show you how to count and display vowels in a string in Python.

Keep reading this Python article to learn different methods to count and display vowels in a string.

  • Using equality operators (==)
  • Using an in-operator
  • Using count() method
  • Using for loop
  • Using list comprehension
  • Using recursion

Let’s see all of them one by one using some illustrative examples.

Python Program to Count the Number of Each Vowel Using Equality Operator (==)

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

This is the example with the source code of Python to count the number of each vowel using the == operator.

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

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, the == operator is used to compare both the operands in Python.

Note: You can also use the .upper() function to check for uppercase for a similar approach.

String = input_string.lower()
for vowel in String:
    if vowel == 'a' or vowel == 'e' or vowel == 'i' or vowel == 'o' or vowel == 'u' :

Output: The screenshot below provides the result once the Python code gets executed.

Count and Display Vowels in a String in Python

Count and Display Vowels in a String in Python using the 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).

The source code of Python is to count the number of vowels in a string using the in operator.

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)

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.

for i in range(len(Phrase)):
    if Phrase[i] in vowels:

Output: You can see the result in the image below after the Python code’s execution.

Python Program to Check Vowel

How to count vowels in a string in Python using the count() method

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

Here is the full code of Python to count the number of vowels in a string using the count() method.

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

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.

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

Refer to the screenshot below. This provides the result of the Python program to count the number of vowels in a string.

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

Count vowels in a given string with Python using for loop

Python uses a for loop to iterate over elements in sequence or 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)

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.

for char in Statement:
    if char in vowels:
        count += 1

Output:

Number of vowels in the given string is:  12

You can refer to the image below to see the result of the Python source code after execution.

Count the Number of Each Vowel in Python

Count the Number of Vowels in Python Using 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.

Here is the complete Python program to count the number of vowels in Python using list comprehension.

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)

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

vowel_count = len([char for char in Quote if char in "aeiouAEIOU"])

Output:

Number of vowels:  16

The below screenshot provides the result of the Python code after execution.

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

Python Program to Count the Number of Vowels Using 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.

Here is the full Python code to count the number of vowels in a string using recursion.

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

Refer to the image below to see the result of the Python program after the code gets executed.

Python program to check vowels using recursion

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 list comprehension, and using recursion.

Now, the choice is yours.

You may also like to read: