Menu Close

Python Program to Count and Display Vowels from a String

Python program to count and display vowels from a string

In this article, we are going to write a Python program to count and display vowels from a string with the help of various examples.
Here we will go through some custom methods we well as use some built-in method to count and display vowels from the string.

Let’s see all the ways of vowels count and display from string one by one.

Python Program to Count and Display Vowels from a String

There are various ways to display vowels from the string which are described below along with proper examples.

  • Using list comprehension
  • Using for loop
  • Using set comprehension
  • Using Dictionary ways
  • Using fromkeys() method

Using list comprehension

Here we will use list comprehension to count and display vowels from a string.

Example: Python program to display and count vowels from a string


string = "My name is Vishvajit Rao and i am a software developer"
vowels = 'aeiou'
result = [v for v in string if v in vowels]
print(''.join(result))
print("Total vowels are: ", len(result))

Output

aeiiaiaoaiaaoaeeeoe
Total vowels are: 19

Using for loop:

Python for loop is another best way to display vowels from a string.

Example: Python program to display vowels from a string


string = "My name is Vishvajit Rao and i am a software developer"
vowels = 'aeiou'
result = ''
for i in string:
    if i in vowels:
        result += i

print("Result is:- ", result)

Output

Result is:- aeiiaiaoaiaaoaeeeoe

Using set comprehension

If you want to display only distinct vowels then you can use set comprehension.set comprehension always starts with a curly bracket { }.

Example: Python program to display vowels from a string


string = "My name is Vishvajit Rao and i am a software developer"
vowels = 'aeiou'
result = {v for v in string if v in vowels}
print(''.join(result))

Output

aeoi

Using Dictionary ways

Dictionary is the best way to count and display vowels from the string. Let see how can I do that?

Example: Python program to count and display vowels from a string


temp = {}
string = "My name is Vishvajit Rao and i am a software developer"
vowels = 'aeiou'

for i in string:
    if i in vowels:
        if temp.get(i):
            temp[i] = temp[i] + 1
        else:
            temp.update({i: 1})
print(temp)

Output

{'a': 7, 'e': 5, 'i': 4, 'o': 3}

Using fromkeys() dictionary method

Use dictionary fromkeys() method to count and display from a string.

Example: Python program to count and display vowels from a string


vowels = 'aeiou'
temp_dict = dict.fromkeys(vowels, 0)
print(temp_dict)

for i in string:
    if i in vowels:
        temp_dict[i] += 1

print(temp_dict)

Output

{'a': 7, 'e': 5, 'i': 4, 'o': 3, 'u': 0}

fromkeys() method reference:- Click Here

Conclsuion

So that, here we have seen all about how to count and display vowels from a string with the help of various examples.
These all are the best ways to count and display vowels from the string. This question might be asked by the interviewer when you are going for a junior Python developer position.

I hope this article will help you. If you like this article, please share and keep visit for further tutorial tutorials.

Thanks for reading…

Previous Article
Next Article

Related Posts