In this article, we learn about how to count vowels in a string python with a video explanation. We cover 3 methods in this article method 1: count vowels in a string python using the iterative, method 2: count vowels in a string python using the recursive, and method 3: count vowels in a string python using the comprehension.
Table of Contents
Video Explanation to Count Vowels in a String Python
Python Program to Count Vowels in a String
Method 1: Count Vowels in a String Python Using the Iterative
def countVowels(str): count = 0 lstVowel = ['A', 'E', 'I', 'O', 'U'] for i in range(len(str)): if str[i].upper() in lstVowel: count += 1 return count str = 'Python programming' print(countVowels(str))
In this program, we have declared variable str with string value “Python programming”. And in the next line, we have called the function countVowelsinside the print statement. Count vowels accept one parameter, which is string str. Let’s check inside the function count vowels; here we have defined variable count and initialized value 0; we will use this for the counter.
In the next line, we have declared a list with all vowels with capital letters and stored them into the variable lstVowel. In the next line, we have a for loop with counter variable i and this loop will run till the length of the string, for that, we have to use the len() function of python.
Then we will check for every character from the variable str. We have if condition and we have used counter variable i, to iterate through str and convert it into upper case and check if the character exists in the list. If the condition is satisfied then inside the condition we will increment the count variable.
After the loop completes, the function returns a count variable, and the system prints the count of vowels. Let’s run this program to see we have our output, which is 4.
Method 2: Count Vowels in a String Python Using the Recursive
def isVowel(ch): return ch.upper() in ['A', 'E', 'I', 'O', 'U'] def countVovels(str, n): if (n == 1): return isVowel(str[n - 1]); return (countVovels(str, n - 1) + isVowel(str[n - 1])) str = "Python programming" print(countVovels(str, len(str)))
Earlier, we studied the iterable method; now, we will study the recursive method. In this program, we have declared variable str with string value “Python programming”. And in the next line, we have called the function countVovels inside the print statement. countVovels accepts two parameters which are string str and length of the string.
Inside the function, we have checked if n, which is the length of the string, is one, then return the count, return call isVowel function with the last character of string if it is a vowel then function return 1 else 0. Else function repeatedly call itself until the length of the string is 1, here we call countVovels function every time, reducing the length of the string plus a count of the vowels.
Let’s run this program. We will get the result 4 as the count of vowels.
Method 3: Count Vowels in a String Python Using the Comprehension
string = "Python programming" vowels = "AaEeIiOoUu" final = [i for i in string if i in vowels] print(len(final))
This is really advanced and a little bit difficult to understand code; let’s kill this difficulty with learning. Here we have the same string as we have in previous programs. In the next line, we have a string of vowels containing both small and capital letters. Here we have initiated list comprehension.
Let’s divide the code into 3 parts.
final = [i for i in string if i in vowels]
Let’s understand this second part for i in string. Here we have started the interaction of the string with variable I. In the next part if i is in vowels we have checked if i is in vowels or not. Which is nothing but a simple condition.
Now let’s come to the first part [i] which is the creation of a list with variable I. In simple terms create a list with vowels from iteration through the string.
Let’s run this and you will get the result as 4.