{"id":7032,"date":"2021-05-25T12:20:48","date_gmt":"2021-05-25T06:50:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=7032"},"modified":"2021-11-22T18:38:42","modified_gmt":"2021-11-22T13:08:42","slug":"python-program-to-count-the-number-of-each-vowel","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-number-of-each-vowel\/","title":{"rendered":"Python Program to Count the Number of Each Vowel"},"content":{"rendered":"
Vowels Definition:<\/strong><\/p>\n A vowel is a sound that you utter with your mouth open, allowing air to pass through it, and is represented in writing by the letters ‘a’, ‘e’, I ‘o’, and ‘u’.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Below are the ways to count the number of each vowel in given string in Python :<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Dictionary:<\/strong><\/p>\n A colon (:) separates each key from its value, commas separate the objects, and the whole thing is enclosed in curly braces. With just two curly braces, an incomplete dictionary with no things is written as follows:<\/p>\n Values may not be unique inside a dictionary, but keys are. A dictionary’s values may be any data type, but the keys must be immutable data types like strings, numbers, or tuples.<\/p>\n Approach:<\/strong><\/p>\n We’ve taken a string from the ip str variable. We make it ideal for caseless comparisons by using the form casefold(). This method basically returns a lowercased version of the string.<\/p>\n We create a new dictionary with each vowel as its key and all values equal to 0 using the dictionary method fromkeys(). This is where the count is started.<\/p>\n We then use a for loop to iterate over the input string.<\/p>\n We check whether the character is in the dictionary keys in each iteration (True if it is a vowel) and increment the value by 1 if it is.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n List Comprehension:<\/strong><\/p>\n In Python, list comprehension is a simple and elegant way to generate a new list from an existing one.<\/p>\n An expression is followed by a for statement enclosed in square brackets in a list comprehension.<\/p>\n Dictionary Comprehension:<\/strong><\/p>\n In Python, dictionary comprehension is a simple and elegant way to construct a new dictionary from an iterable.<\/p>\n Dictionary comprehension is made up of an expression pair (key: value) followed by a for statement enclosed in curly braces{}.<\/p>\n This method produces the same output as the previous one.<\/p>\n However, since we iterate over the entire input string for each vowel, this program is slower.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Vowels Definition: A vowel is a sound that you utter with your mouth open, allowing air to pass through it, and is represented in writing by the letters ‘a’, ‘e’, I ‘o’, and ‘u’. Examples: Example1: Input: string = “croyez” Output: a : 0 e : 1 i : 0 o : 1 u : …<\/p>\nstring = \"croyez\"<\/pre>\n
a : 0\r\ne : 1\r\ni : 0\r\no : 1\r\nu : 0<\/pre>\n
Here the letters o, e are vowels and o is occurred once and e is occurred once<\/pre>\n
string =\"Hello this is BTechGeeks\"<\/pre>\n
a : 0\r\ne : 4\r\ni : 2\r\no : 1\r\nu : 0<\/pre>\n
string =\"ELEPHANT\"<\/pre>\n
a : 1\r\ne : 2\r\ni : 0\r\no : 0\r\nu : 0<\/pre>\n
Here the letters e ,a are vowels and e is repeated twice and a is repeated once.\r\nHere we ignore the case while counting vowels<\/pre>\n
Count the Number of Each Vowel in Python<\/h2>\n
\n
Method #1:Using Dictionary<\/h3>\n
# Taking a string which have total vowels\r\nvowelstring = 'aeiou'\r\n\r\ngiven_string = 'Hello this is BTechGeeks'\r\n\r\n# making the string suitable for caseless comparisions\r\ngiven_string = given_string.casefold()\r\n# making a dictionary with keys as vowel and the value as 0\r\ncountVowels = {}.fromkeys(vowelstring, 0)\r\n\r\n# counting the vowels in given string\r\nfor char in given_string:\r\n # increment the count in dictionary if it is vowel\r\n if char in countVowels:\r\n countVowels[char] += 1\r\n# print the count\r\nfor key, value in countVowels.items():\r\n print(key, \":\", value)\r\n<\/pre>\n
a : 0\r\ne : 4\r\ni : 2\r\no : 1\r\nu : 0<\/pre>\n
Method #2: Using list and dictionary comprehension in python<\/h3>\n
\n
# Taking a string which have total vowels\r\nvowelstring = 'aeiou'\r\n\r\ngiven_string = 'Hello this is BTechGeeks'\r\n\r\n# making the string suitable for caseless comparisions\r\ngiven_string = given_string.casefold()\r\n\r\n# counting the vowels in given string\r\ncountVowels = {x: sum([1 for char in given_string if char == x])\r\n for x in 'aeiou'}\r\n# print the count\r\nfor key, value in countVowels.items():\r\n print(key, \":\", value)\r\n<\/pre>\n
a : 0\r\ne : 4\r\ni : 2\r\no : 1\r\nu : 0<\/pre>\n
\n