Python Count vowels in a string

Here, we will figure out how to include vowels in a string in Python? Here, we have a string with vowels and consonants, we need to tally the vowels.

Given a string, and we need to check the all outnumber of vowels in the string utilizing python program.

Example:

    Input:
    Str = "Hello world"

    Output:
    Total vowels are: 3

Program:

# count vowels in a string 

# declare, assign string 
str = "Hello world"

# declare count 
count = 0

# iterate and check each character
for i in str:
	# check the conditions for vowels
	if( i=='A' or i=='a' or i=='E' or i=='e'
	or i=='I' or i=='i' or i=='O' or i=='o'
	or i=='U' or i=='u'):
		count +=1;
		
# print count
print "Total vowels are: ", count

Output:

    Total vowels are:  3

Actualize the program by making capacities to check vowel and to tally vowels:

Here, we are making two capacities:

1) isVowel()

This capacity will accept the character as contention and returns True if the character is a vowel.

2) countVowels()

This capacity will accept a string as a contention, and return a complete number of vowels of the string.

Program:

# count vowels in a string 

# function to check character
# is vowel or not 
def isVowel(ch):
    # check the conditions for vowels 
	if(ch=='A' or ch=='a' or ch=='E' or ch=='e'
	or ch=='I' or ch=='i' or ch=='O' or ch=='o'
	or ch=='U' or ch=='u'):
		return True
	else:
		return False

# function to return total number of vowels
def countVowel(s) :
	# declare count 
	count =0
	# iterate and check characters
	for i in str:
		if(isVowel(i) == True):
			count += 1 
	return count
	
# Main code 
# declare, assign string
str = "Hello world"

# print count 
print "Total vowels are: ", countVowel(str)

Output:

    Total vowels are:  3

Leave a Comment

error: Alert: Content is protected!!