Count the number of vowels in a string



Hello guys this is Satyam,again back with another awesome blog and in this blog I am going to show you how you can count the number of vowels in a given string by the user.So this will be an easy Python program,this program will be based on the user of list and membership operator.

Code:
def count_vowels(s):
     count = 0
     for i in s: 
           if i in ['a','e','i','o''u']:
                  count+=1
     return count

print(count_vowels("python"))


So the above code will help you to count the number of vowels in a given string but guys we can do the same using another method in which we will use the or operator to check the values of i,for different vowels,see the code for more clear understanding:

Code:
def count_vowels(s):
     count = 0
     for i in s: 
           if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
                  count+=1
     return count

print(count_vowels("python"))


So this was it guys,hope you liked it,follow and subscribe this blog for latest post notification.



Thanks for Reading



                    

No comments: