Python Program to Count Vowels and Consonants in a String

Write a Python program to Count Vowels and Consonants in a String using For Loop and ASCII values with a practical example.

Python Program to Count Vowels and Consonants in a String Example 1

This python program allows the user to enter a string. Next, it counts the total number of vowels and consonants in this string using For Loop. First, we used Python For Loop to iterate each character in a String. Inside the For Loop, we are using If Statement to check the String character is a, e, i, o, u, A, E, I, O, U. If true, increment the vowels value otherwise, increment the consonant value

# Python Program to Count Vowels and Consonants in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0
consonants = 0

for i in str1:
    if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'
       or i == 'A' or i == 'E' or i == 'I' or i == 'O' or i == 'U'):
        vowels = vowels + 1
    else:
        consonants = consonants + 1
 
print("Total Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", consonants)

Python Count Vowels and Consonants in a String output

Please Enter Your Own String : Hello WOrld
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  8
>>> 
Please Enter Your Own String : Python Programs
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  12

Program to Count Vowels and Consonants in a String Example 2

In this program program, we are using the lower function to cover the string to Lowercase. By this, you can only use a, e, i, o, u inside the Python If statement (avoid uppercase letters).

# Python Program to Count Vowels and Consonants in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0
consonants = 0
str1.lower()

for i in str1:
    if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'):
        vowels = vowels + 1
    else:
        consonants = consonants + 1
 
print("Total Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", consonants)
Python Program to Count Vowels and Consonants in a String 2

Python Program to Count Total Vowels and Consonants Example 3

This program uses ASCII values to find vowels and consonants. I suggest you to refer ASCII table article to understand ASCII values.

# Python Program to Count Vowels and Consonants in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0
consonants = 0
str1.lower()

for i in str1:
    if(ord(i) == 65 or ord(i) == 69 or ord(i) == 73
       or ord(i) == 79 or ord(i) == 85
       or ord(i) == 97 or ord(i) == 101 or ord(i) == 105
       or ord(i) == 111 or ord(i) == 117):
        vowels = vowels + 1
    elif((ord(i) >= 97 and ord(i) <= 122) or (ord(i) >= 65 and ord(i) <= 90)):
        consonants = consonants + 1
 
print("Total Number of Vowels in this String = ", vowels)
print("Total Number of Consonants in this String = ", consonants)

Python Count Vowels and Consonants in a String output

Please Enter Your Own String : Python Examples
Total Number of Vowels in this String =  4
Total Number of Consonants in this String =  10
>>> 
Please Enter Your Own String : Learn Python Programming
Total Number of Vowels in this String =  6
Total Number of Consonants in this String =  16
>>>