Python program to count the number of vowels in a string
Count the number of vowels in a string
Program to Count the Number of vowels we’re going to check how many vowels are present in a given String . There are five vowels in English vocabulary, they are – ‘a’, ‘e’, ‘i’, ‘o’, ‘u’.
For Example, in the string prepinsta then in that case then vowesl are 3 (a,e,i)
Algorithm
- Step 1:- Start.
- Step 2:- Take user input.
- Step 3:- Initialize count variable.
- Step 4:- Iterate through the string to find number of vowels.
- Step 5:- Check if the alphabet of the string lies under the group of vowels.
- Step 6:- If TRUE increment count by 1.
- Step 7:- Print count.
- Step 8:- End.
Python program to count number of vowels in a string
Run
#take user input String = input('Enter the string :') count = 0 #to check for less conditions #keep string in lowercase String = String.lower() for i in String: if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u': #if True count+=1 #check if any vowel found if count == 0: print('No vowels found') else: print('Total vowels are :' + str(count))
Output: Enter the string :PrepInsta Total vowels are :3
Note
The time complexity of above code in O(n) as in the code we are looping over the sting once
Method 2
Objective: Find number of vowels in string python
- First take the string input in a string variable and make a function countVowels(str,str.length())
- if the value of n that is (length of the string) is 1 then we will return. (This is the base case)
- if the size of the string is not 1 then countVowels(str, n-1) + isVowel(str[n-1])
- Here isVowel(str[n-1]) will be called if the character is vowel then this will return 1 otherwise 0
Objective: Given an alphabetical string ‘s’. count the number of vowels in it.
Run
# Function to check the Vowel def isVowel(ch): return ch.upper() in ['A', 'E', 'I', 'O', 'U'] # to count total number of # vowel from 0 to n def countVovels(str, n): if (n == 1): return isVowel(str[n - 1]); return (countVovels(str, n - 1) + isVowel(str[n - 1])); # Driver Code # string object str = "prepinsta"; # Total numbers of Vowel print("Total numbers of Vowel =",countVovels(str, len(str)))
Total numbers of Vowel = 3
Note
recursively calling the function so here the time complexity of the code is O(n)
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
input=”chowdeswari”
output=0
for i in input:
if i in [“a”,”e”,”i”,”o”,”u”,”A”,”E”,”I”,”O”,”U”]:
output+=1
print(output)
word = ‘abcgdGbEvTs’
word = word.lower()
vowels = [‘a’,’e’,’i’,’o’,’u’]
count = 0
for i in word:
for j in vowels:
if i == j:
count += 1
print(count)
s = input(‘enter a value’)
t = 0
for i in s:
if i in ‘aeiouAEIOU’:
t+=1
print(‘The number of vowels in string is:’,t)
print(len([i for i in input(“enter a string – “) if i in [“a”, “A”, “e”, “E”, “i”, “I”, “o”, “O”, “u”, “U”]]))
a=input()
l=[‘a’,’e’,’i’,’o’,’u’,’A’,’E’,’I’,’U’,’O’]
count=0
for i in a:
if i in l:
count+=1
print(count)
arr=[‘a’,’e’,’i’,’o’,’u’]
arr2=[]
s=str(input())
for i in s:
if i in arr:
arr2.append(i)
print(len(arr2))
str1=str(input())
count=0
for j in str1:
if j==’a’ or j==’A’:
count+=1
elif j==’e’ or j==’E’:
count+=1
elif j==’i’ or j==’I’:
count+=1
elif j==’o’ or j==’O’:
count+=1
elif j==’u’ or j==’U’:
count+=1
if(count==0):
print(‘No vowels found’)
print(count)
txt = input(‘Enter the String :’)
txt = txt.lower()
c = 0
vowel = ‘aeiou’
for i in vowel:
if i in txt:
c += 1
print(c)
name = str(input(“enter your string:”))
count = 0
for char in name:
if char in “aeiouAEIOU”:
count = count + 1
print(count)
st=input(“Enter the string : “)
c=0
vowel=’AEIOUaeiou’
for i in st:
if i in vowel:
c+=1
print(c)