Home » Python programs

Python | Count vowels in a string



Here, we are going to learn how to count vowels in a string in Python? Here, we have a string with vowels and consonants, we have to count the vowels.
Submitted by IncludeHelp, on August 01, 2018

Given a string, and we have to count the total number of vowels in the string using 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

Implement the program by creating functions to check vowel and to count vowels:

Here, we are creating two functions:

1) isVowel()

This function will take character as an argument, and returns True, if character is vowel.

2) countVowels()

This function will take string as an argument, and return total 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Quick links
Latest articles, Internship, Members
New...
Coding problems, Algorithms, Discrete Mathematics, Big data
Languages
C, C++, C++ STL, Java, Data Structure, C#.Net, Android, Kotlin, SQL
Web
PHP, Python, JavaScript, CSS, Ajax, Node.js, Web prog.
Programs
C, C++, DS, Java, C#, Python
Aptitude
C, C++, Java, DBMS
Interview
C, Embedded C, Java, SEO, HR
CS Subjects
CS Basics O.S. Networks DBMS Embedded Systems Cloud Computing Machine learning CS Organizations Linux DOS
More...
Articles, Puzzles, News/Updates


Recommended posts
C Tips & Tricks, C++ Tips & Tricks
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distros of 2018
C programming optimization techniques
Differences b/w C & Embedded C?
Embedded C Interview Q. & A.
C programming tips for Embedded Development
Basic rules of writing a C program
Important points (rules) to remember while writing C/C++ program
Top 5 Websites for solving programming challenges
Read more...


Others...
Computer G.K. (MCQ)
Most viewed pages...
Categories...



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.