Python program to count the number of vowels using sets in a given string



In this article, we will learn about the solution to the problem statement given below.

Problem statement − We are given a string, we need to count the number of vowels using sets in a given string.

Here we are traverse through the whole string and check whether each character is a vowel or not and increment the count.

Now let’s observe the concept in the implementation below −

Example

 Live Demo

def vowel_count(str):
   count = 0
   #string of vowels
   vowel = "aeiouAEIOU"
   for alphabet in str_:
      #check whether character is a vowel or not
      if alphabet in vowel:
         count = count + 1
   print("No. of vowels in the given string:", count)
# Driver code
str_ = "Tutorialspoint"
vowel_count(str)

Output

No. of vowels in the given string : 6

All the variables are declared in the local scope and their references are seen in the figure above.

Conclusion

In this article, we have learned about how we can count the number of vowels in a given string using sets.


Advertisements