![]() |
VOOZH | about |
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.
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.
Counter({'o': 1, 'i': 1, 'u': 1})
Explanation:
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.
Counter({'o': 1, 'i': 1, 'u': 1})
Explanation:
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.
Counter({'o': 1, 'i': 1, 'u': 1})
Explanation:
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.
{'i': 1, 'o': 1, 'u': 1}
Explanation: