How to Count the Vowels in a String in Python

We want you to know about the Count Vowels from a string in Python, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

You still face difficulties in understanding the logic? That’s perfectly fine, we have something more for you. You can scroll down to learn more about this topic.

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to Count Vowels from a string in Python

Code: Count Vowels from a string in Python

Method 1: Count Vowels from a string using Iterative method in Python

Source code:

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))

Output:

4

Code Explanation Method 1: Count Vowels from a string using Iterative method in Python

In this program, we have declared a variable str with string value “Python programming”.

And in the next line we have called the function countVowelsinside the print statement.

countVovels accepts one parameter which is string str.

Let’s check inside the function countVovels, here we have defined variable count and initialize 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 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 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 see we have our output, which is 4

Method 2: Count Vowels from a string using recursive method in Python

Source code:

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)))

Output

4

Code Explanation Method 2: Count Vowels from a string using recursive method in Python

Earlier we studied the iterable method, now we will learn 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 length of the string is 1, here we call countVovels function every time reducing length of string plus count of the vowels.

Let’s run this program. We will get the result 4 as the count of vowels.

Method 3: Count Vowels from a string using comprehension method in Python

Source code:

string = "Python programming"
vowels = "AaEeIiOoUu"
final = [i for i in string if i in vowels]
print(len(final))

Output

4

Code Explanation Method 3: Count Vowels from a string using comprehension method in Python

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 like 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]

  • [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 string with variable i.

In the next part if i 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 string.

Let’s run this and you will get result as 4.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *