Open In App

Python - Count and Display Vowels In a String

Last Updated : 11 Dec, 2025
Comments
Improve
Suggest changes
22 Likes
Like
Report

Given a string, the task is to identify all vowels (both uppercase and lowercase), display each vowel found, and count how many times every vowel occurs. For Example:

Input: "Python is Fun!"
Output: {'o': 1, 'i': 1, 'u': 1}
Explanation: The vowels present in the string are o, i, and u, each appearing once.

Let's explore different methods to do this task in Python.

Using collections.Counter with List Comprehension

This method first collects all vowels from the string into a list using a comprehension. Counter then takes that list and counts how many times each vowel appears.

Python
from collections import Counter

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

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

Explanation:

  • [ch for ch in s if ch in v] collects only characters that are vowels.
  • Counter() takes that list and counts how many times each vowel appears.

Using Regular Expressions (re.findall)

This method uses a regular expression pattern that matches only vowels. findall scans the entire string and returns a list containing every vowel found. Once the list is created, Counter is used to count each vowel.

Python
import re
from collections import Counter

s = "Python is Fun!"
pattern = r"[aeiouAEIOU]"
vowels = re.findall(pattern, s)
res = Counter(vowels)
print(res)

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

Explanation:

  • re.findall() searches the string for the pattern [aeiouAEIOU] and returns all matched vowels.
  • The returned list is passed to Counter() to get the final frequency of each vowel.

Using filter() with a Lambda Function

This method filters the string by checking each character through a lambda function. The filter keeps only characters that belong to the vowel list. Once the filtered list is ready, Counter counts the occurrences of each vowel.

Python
from collections import Counter

s = "Python is Fun!"
v = "aeiouAEIOU"
flt = list(filter(lambda c: c in v, s))
res = Counter(flt)
print(res)

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

Explanation:

  • filter(lambda c: c in v, s) keeps only characters that are in the vowel list.
  • Counter() then counts the filtered characters.

Using str.count() for Each Vowel

This method checks each vowel individually. If a vowel appears in the string, str.count() returns how many times it occurs, and that count is added to the final dictionary.

Python
s = "Python is Fun!"
v = "aeiouAEIOU"
res = {ch: s.count(ch) for ch in v if ch in s}
print(res)

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

Explanation:

  • dictionary comprehension loops over each vowel.
  • For every vowel found in the string, s.count(ch) returns how many times it appears.

Explore