- Python Basic Programs
- Python Program Examples
- Python Print Hello World
- Python Get Input from User
- Python Add Two Numbers
- Add Subtract Multiply Divide
- Python Check Even or Odd
- Python Check Prime or Not
- Python Check Alphabet or Not
- Python Check Vowel or Not
- Python Check Leap Year or Not
- Check Reverse equal Original
- Check Positive Negative Zero
- Python Check Armstrong or Not
- Python Check Palindrome or Not
- Python Check Perfect Number
- Python Find Reverse of Number
- Python Count Digits in Number
- Python Add Digits of Number
- Sum of First and Last Digits
- Python Product of Mid Digits
- Sum of Squares of Digits
- Interchange Digits of Number
- Python Sum of n Numbers
- Python Print ASCII Values
- Python Swap Two Numbers
- Python Swap Two Variables
- Python Fahrenheit to Celsius
- Python Celsius to Fahrenheit
- Python Display Calendar
- Python Days into Years, Weeks
- Find Largest of Two Number
- Find Largest of Three Number
- Python Print Fibonacci Series
- Generate Armstrong Numbers
- Python Make Simple Calculator
- Python Add Binary Numbers
- Binary Number Multiplication
- Python Mathematical Programs
- Find Sum of Natural Numbers
- Find Average of n Numbers
- Python Print Multiplication Table
- Print Table using Recursion
- Python Find Average Percentage
- Python Find Grade of Student
- Find Square Root of Number
- Python Print Prime Numbers
- Find Numbers Divisible by
- Python Find Factors of Number
- Python Find Factorial of a Number
- Python Find HCF & LCM
- Python Kilometres to Miles
- Python Find Area of Square
- Python Find Area of Rectangle
- Python Find Area of Triangle
- Python Find Area of Circle
- Python Find Perimeter of Square
- Find Perimeter of Rectangle
- Python Find Perimeter of Triangle
- Find Circumference of Circle
- Python Simple Interest
- Python Solve Quadratic Equation
- Python Different Set of Operations
- Python Display Powers of 2
- Python Find nCr & nPr
- Python Pattern Programs
- Python Print Pattern Programs
- Python Print Diamond Pattern
- Python Print Floyd's Triangle
- Python Print Pascal's Triangle
- Python List Programs
- Python Count Even/Odd in List
- Python Positive/Negative in List
- Python Even Numbers in List
- Python Odd Numbers in List
- Python Sum of Elements in List
- Sum of Odd/Even Numbers
- Python Element at Even Position
- Python Element at Odd Position
- Python Search Element in List
- Python Largest Number in List
- Python Smallest Number in List
- Python Second Largest in List
- Python Second Smallest in List
- Python Insert Element in List
- Python Delete Element from List
- Python Multiply Numbers in List
- Swap Two Elements in List
- Python 1D Array Program
- Python Linear Search
- Python Binary Search
- Python Insertion Sort
- Python Bubble Sort
- Python Selection Sort
- Remove Duplicates from List
- Python Reverse a List
- Python Merge Two List
- Python Copy a List
- Python Conversion Programs
- Python Decimal to Binary
- Python Decimal to Octal
- Python Decimal to Hexadecimal
- Python Binary to Decimal
- Python Binary to Octal
- Python Binary to Hexadecimal
- Python Octal to Decimal
- Python Octal to Binary
- Python Octal to Hexadecimal
- Python Hexadecimal to Decimal
- Python Hexadecimal to Binary
- Python Hexadecimal to Octal
- Python Matrix Programs
- Python Add Two Matrices
- Python Subtract Two Matrices
- Python Transpose Matrix
- Python Multiply Matrices
- Python String Programs
- Python Print String
- Python Find Length of String
- Python Compare Two Strings
- Python Copy String
- Python Concatenate String
- Python Reverse a String
- Python Swap Two Strings
- Python Uppercase to Lowercase
- Python Lowercase to Uppercase
- Python Check Substring in String
- Python Count Character in String
- Count Repeated Characters
- Python Count Word in Sentence
- Python Count Each Vowels
- Python Capitalize Character
- Python Capitalize Word in String
- Python Smallest/Largest Word
- Remove Spaces from String
- Remove Duplicate Character
- Remove Vowels from String
- Remove Punctuation from String
- Python Remove Word in String
- Python Remove Duplicate Words
- WhiteSpace to Hyphens
- Replace Vowels with Character
- Replace Character in String
- Python Sort String in Alphabetical
- Sort Word in Alphabetical Order
- Extract Number from String
- Python Check Anagram Strings
- Python File Programs
- Python Read a File
- Python Write to File
- Python Append Text to File
- Python Copy Files
- Python Merge Two Files
- Python Counts Characters in File
- Python Count Words in File
- Python File Content in Reverse
- Python Lines Contains String
- Python Delete Line from File
- Python Capitalize Word in File
- Python Replace Text in File
- Replace Specific Line in File
- Python Find Size of File
- Python List Files in Directory
- Python Delete Files
- Python Misc Programs
- Python Reverse a Tuple
- Python Merge Two Dictionary
- Python bytes to String
- Python bytearray to String
- Generate Random Numbers
- Python Print Address of Variable
- Python Print Date and Time
- Python Get IP Address
- Python Shutdown/Restart PC
- Python Tutorial
- Python Tutorial
Python Program to Count Number of Each Vowels in String
This article is created to cover some programs in Python, that count and prints number of each vowels in a string entered by user. Here are the list of approaches used:
- Count number of each vowels in a string using, for loop and if-elif statements
- Using list
- Using dictionary
Count Each Vowels using for Loop and if-else
This program count and prints number of each vowels present in a string, using for loop and if-elif statements. The question is, write a Python program to count number of each vowels. Here is its answer:
print("Enter the String:") text = input() vowela = ['a', 'A'] vowele = ['e', 'E'] voweli = ['i', 'I'] vowelo = ['o', 'O'] vowelu = ['u', 'U'] ca = 0 ce = 0 ci = 0 co = 0 cu = 0 for x in text: if x in vowela: ca = ca+1 elif x in vowele: ce = ce+1 elif x in voweli: ci = ci+1 elif x in vowelo: co = co+1 elif x in vowelu: cu = cu+1 print("\n'a' occurs ", ca) print("'e' occurs ", ce) print("'i' occurs ", ci) print("'o' occurs ", co) print("'u' occurs ", cu)
This program produces the following output:
Now supply the input say Welcome to jobails.com as string, then press ENTER
key to
count and print number of each vowels in given string as shown in the snapshot given below:
The dry run of above program with same user input as provided in above sample run, goes like:
- Initial values, text = "Welcome to jobails.com" (entered by user), ca=0, ce=0, ci=0, co=0, cu=0
- Now the execution of for loop begins, that is, the following code:
for x in text:
states that, one by one, each characters gets copied to x. That is, at first, "W" (the very first character of text) gets copied to x, at second "e" gets copied to x, and so on upto the last character of text - Inside the loop, the following code:
if x in vowela:
states that if value of x is available in the list named vowela, then the condition (of if) evaluates to be true, and program flow goes inside its body and the value of ca gets incremented by 1 - Therefore, at first evaluation of for loop, x="W". And "W" is not available in any of all five lists namely ca, ce, ci, co, and cu
- Therefore, x="e", the second character gets copied to x and again checks for matching
- This time, "e" is available in the list vowele, therefore the condition (of first elif) x in vowele or "e" in ['e', 'E'] evaluates to be true, therefore program flow goes inside this elif's body and ce+1 or 0+1 or 1 gets initialized to ce
- This process continues, until each and every characters gets scanned and checked
- In this way, all five vowels gets counted. So just print its value after exiting from the loop
Modified Version of Previous Program
This program is the modified version of previous. This program uses end to skip inserting an automatic newline using print()
print("Enter String: ", end="") text = input() v = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] va = ['a', 'A'] ve = ['e', 'E'] vi = ['i', 'I'] vo = ['o', 'O'] vu = ['u', 'U'] ca = 0 ce = 0 ci = 0 co = 0 cu = 0 for x in text: if x in v: if x in va: ca += 1 elif x in ve: ce += 1 elif x in vi: ci += 1 elif x in vo: co += 1 elif x in vu: cu += 1 print() if ca>0: print("\"a\" occurs", ca, "times") if ce>0: print("\"e\" occurs", ce, "times") if ci>0: print("\"i\" occurs", ci, "times") if co>0: print("\"o\" occurs", co, "times") if cu>0: print("\"u\" occurs", cu, "times")
Here is its sample run with user input, HellO, wElcomE tO jobails.com as string:
Note - The \" is used to print "
Count Number of Each Vowels using List
This program uses list to do the same job as of previous program. The list is used in a way that, at 0th index, frequency of 'a' gets stored, at 1st index, frequency of 'e' gets stored, and so on in sequence of a, e, i, o, u. The count[0] refers to value (element) at 0th index of list named count.
print("Enter String: ", end="") text = input() count = [0, 0, 0, 0, 0] text = text.casefold() vowels = ['a', 'e', 'i', 'o', 'u'] for ch in text: if ch in vowels: if ch=='a': count[0] += 1 elif ch=='e': count[1] += 1 elif ch=='i': count[2] += 1 elif ch=='o': count[3] += 1 elif ch=='u': count[4] += 1 print() for i in range(len(count)): if i==0: if count[i]>0: print("'a' occurs", count[i], "time(s)") if i==1: if count[i]>0: print("'e' occurs", count[i], "time(s)") if i==2: if count[i]>0: print("'i' occurs", count[i], "time(s)") if i==3: if count[i]>0: print("'o' occurs", count[i], "time(s)") if i==4: if count[i]>0: print("'u' occurs", count[i], "time(s)")
Here is its sample run with jobails.com as string input:
Note - The casefold() method is used to convert whole string passed as its argument, to lowercase.
Count Each Vowels using Dictionary
This is the last program of this article, uses dictionary to count number of each vowel in a string entered by user at run-time. Dictionary is used to store information in key:value pairs
print("Enter String: ", end="") text = input() vowels = ['a', 'e', 'i', 'o', 'u'] text = text.casefold() count = {}.fromkeys(vowels, 0) print() for ch in text: if ch in count: count[ch] += 1 print(count)
Here is its sample run with user input as of previous program's sample run:
Note - Dictionary are defined using {} (curly) braces.
Note - The fromkeys() method returns dictionary with specified keys and value. That is, the first argument refers to keys, whereas the second argument refers to a value.
The following statement (from above program):
count[ch] += 1
fetches value with specified key, that is ch and increments the value by 1.
« Previous Program Next Program »