Home » Count vowels in a string – Python Program

Count vowels in a string – Python Program

Count vowels in a string – Python Program

Hello everyone, welcome back to programminginpython.com! Here in this tutorial am going to tell how you can count vowels in a string when an input string is received you will be able to count the number of vowels present in that string.

Master the basics of data analysis in Python. Expand your skillset by learning scientific computing with numpy.

Take the course on Introduction to Python on DataCamp here https://bit.ly/datacamp-intro-to-python

Program on Github

You can also watch the video on YouTube here.

Task

Python program to count the number of each vowel in a given string.

Approach

  • Read an input string using input() or raw_input().
  • Declare vowels as a string `vowels = ‘aeiou’`
  • Make the input string case insensitive using `casefold()` method
  • Create a dictionary with each vowel a key and value 0
  • Loop the string and check for vowels and if found increment the value in the dictionary
  • Print the result(dictionary)

Program

__author__ = 'Avinash'

# Python3 program to count vowels in a string

# string of vowels
vowels = 'aeiou'
input_str = input("Enter a string: ")
# make input string case insensitive
input_str = input_str.casefold()
# make a dictionary with each vowel a key and value 0
vowels_count = {}.fromkeys(vowels, 0)
# count the number of each vowels
for letter in input_str:
    if letter in vowels_count:
        vowels_count[letter] += 1
print(vowels_count)

 

Output

Count Vowels in a String

Count Vowels in a String

Count Vowels in a String – Code Visualization

Course Suggestion

Machine Learning is everywhere! So I strongly suggest you to take the course below.
Course: Machine Learning Adv: Support Vector Machines (SVM) Python

 

Feel free to check other Python programs like sorting and searching algorithms or pattern programming and many more programs here.

Pattern Programming – Letter Series

Online Python Compiler

Leave a Reply

Your email address will not be published. Required fields are marked *