Thanks to visit codestin.com
Credit goes to www.geeksforgeeks.org

Open In App

Python – Count and display vowels in a string

Last Updated : 26 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string, we need to write a Python program that counts and displays all the vowels present in the string. Let’s explore different ways of counting and displaying vowels in a string.

  • We need to identify all the vowels in the string both uppercase and lowercase.
  • We should display each vowel found.
  • We should also count how many total vowels are in the string.

Let’s solve the problem with the most efficient approach:

Using collections.counter with a List Comprehension

The collections.counter class is a highly efficient way to count occurrences of vowels in a string.

Python
from collections import Counter

s = "Python is Fun!"
v = "aeiouAEIOU"
cnt = Counter([i for i in s if i in v])
print(cnt)

Output
Counter({'o': 1, 'i': 1, 'u': 1})

Explanation:

  • List Comprehension: [i for i in s if i in v] picks out only the vowels from s, creating a list like [‘o’, ‘i’, ‘u’].
  • Count with Counter: cnt = Counter([i for i in s if i in v]) then counts how many times each vowel appears.

Other methods of counting and displaying vowels in a string is as follows:

Using str.count() in a loop

The str.count() method can be used iteratively to count each vowel’s occurrences in the string. This method is more readable and directly uses built in string methods.

Python
s = "Python is fun!"
vowels = "aeiouAEIOU"
cnt = {i: s.count(i) for i in vowels if i in s}
print(cnt)

Output
{'i': 1, 'o': 1, 'u': 1}

Explanation:

  • Dictionary Comprehension: Iterates over each vowel i in vowels. Checks if that vowel i is present in s (if i in s).
  • If present, creates an entry in the dictionary cnt where the key is the vowel i and the value is s.count(i)—the number of times i appears in s.

Using Regular Expressions

Regular expressions (re) are used for pattern matching and can be used to count vowels in a single step.

Python
import re

s = "Python is fun!"
p = r'[aeiouAEIOU]'
vowel = re.findall(p, s)
print(f"Count: {len(vowel)}, Vowels: {vowel}")

Output
Count: 3, Vowels: ['o', 'i', 'u']

Explanation:

  • The re.findall() function locates all vowels in the string based on the regex pattern [aeiouAEIOU].
  • len() provides the total count of vowels, and the matched list shows the vowels found.

Using filter()

The filter() function, combined with a lambda, can extract vowels from the string for processing. These are ideal for situations where we prefer filtering vowels before counting.

Python
s = "Python is fun!"
vowels = "aeiouAEIOU"
fv = list(filter(lambda char: char in vowels, s))
cnt = {char: fv.count(char) for char in set(fv)}
print(cnt)

Output
{'u': 1, 'i': 1, 'o': 1}

Explanation:

  • filter() uses a lambda function to extract vowels from the string.
  • fv.count() is applied within a dictionary comprehension to get counts of each vowel.

Using a Manual Loop

A manual loop allows for full control over how we count and display vowels.

Python
s = "Python is fun!"
vowels = "aeiouAEIOU"
counts = {}
for char in s:
    if char in vowels:
        counts[char] = counts.get(char, 0) + 1
print(counts)

Output
{'o': 1, 'i': 1, 'u': 1}

Explanation:

  • Each character in the string is checked against the set of vowels.
  • The get() method initializes counts and increments them when a vowel is found.


Similar Reads