How to count vowels in python?

The letter 'a', 'e', 'i', 'o', 'u' represents vowel in English alphabets. The article discusses various ways to count vowels in a string in python.

Counting vowels using or operator

The or operator is a Logical operator that returns a boolean value True if either of the operands is True. Otherwise, it returns False.

Consider a string string_1 = "ChErcher Tech". All the characters in a string_1 are first converted to lowercase letters using the lower() function and are assigned to string_2 so that all the uppercase and lowercase vowels are counted. A count variable is initialized to 0.

A for loop traverses the string_2. If condition checks if the character is equal to any of the vowels 'a', 'e', 'i', 'o', 'u' using or operator. If the character matches any of the vowels, the variable count is incremented by 1.

string_1 = "ChErcher Tech"
print("string_1: ", string_1)
string_2 = string_1.lower()
print("string_2: ", string_2)
count = 0
for ch in string_2:
    if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u':
        count += 1
print("Total vowels in string_ 1:", count)

Output

string_1:  ChErcher Tech
string_2:  chercher tech
Total vowels in string_1: 3

Counting vowels using set and in operator

A set is a collection of data items, separated by commas and enclosed within curly brackets. For eg. {'a', 'e', 'i', 'o', 'u'} represents a set. The in operator checks the existence of an element in an iterable and returns a boolean value.

Consider a string string_1 = "PythOn PrOgramming". All the characters in a string_1 are first converted to lowercase letters using the lower() function and are assigned to string_2 so that all the uppercase and lowercase vowels are counted. The vowels are represented as a set and stored in vowel_set= {'a', 'e', 'i', 'o', 'u'}. A count variable is initialized to 0.

The in operator checks existence of each character of the string in vowel_set. If the character exists, the variable count is incremented by 1.

string_1 = "PythOn PrOgramming"
print("string_1: ", string_1)
string_2 = string_1.lower()
print("string_2: ", string_2)
vowel_set = {'a', 'e', 'i', 'o', 'u'}
count = 0
for ch in string_2:
    if ch in vowel_set:
        count+=1
print("Total vowels in string_1:", count)

The above code gives output as

string_1:  PythOn PrOgramming
string_2:  python programming
Total vowels in string_1: 4

Counting vowels by replacing the vowels with an empty string

We use re.sub() method to replace vowels in the string. The re.sub() method can be imported from the re(Regular expressions) module. The re.sub() returns a string by replacing a set of one or more characters with a replacement string. The syntax for re.sub() is given below.

#syntax:
re.sub(pattern, repl, string)
  • pattern: The pattern indicates the set of characters to be replaced.
  • repl: This takes a string as an argument that would replace the characters.
  • string: This represents the original string.

Consider a string string_1 = "Programming". re.sub("[AEIOUaeiou]", '',string_1) replace all the vowels in string_1 with an empty string and returns a new string to the variable string_2.

The length of string_1 and string_2 is calculated using the len() function. The difference between the length of the string_1 and string_2 gives the number of vowels in the string_1.

import re
string_1 = "Programming"
print("string_1: ", string_1)
string_2 = re.sub("[AEIOUaeiou]", '', string_1)
print("string_2: ", string_2)
print("Length of string_1: ", len(string_1))
print("Length of string_2: ", len(string_2))
print("Total vowels: ", len(string_1) - len(string_2))   

Output

string_1:  Programming
string_2:  Prgrmmng
Length of string_1:  11
Length of string_2:  8
Total vowels:  3

Counting the number of each vowel in a string using a dictionary

Dictionary is a built-in datatype in python that stores the collection of data as key-value pairs. The key-value pairs in the dictionary are enclosed within curly brackets {}.

Consider a string string_1 = "ChErcher Tech". A dictionary Dict_1 is initialized to {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}. All the characters in the string_1 are first converted to lowercase letters using the lower() function and are assigned to string_2 so that all the uppercase and lowercase vowels are counted.

A for loop iterates over the string_2 and check for the character in the keys of Dict_1. If the character is present in the keys the value of that particular key is incremented by 1.

string_1 = "ChErcher Tech"
print("string_1 : ", string_1)
Dict_1 = {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
string_2 = string_1.lower()
print("string_2 : ", string_2)
for ch in string_2:
    if ch in Dict_1:
        Dict_1[ch] += 1
print("The updated Dict_1: ", Dict_1)

The above code returns the output as

string_1 :  ChErcher Tech
string_2 :  chercher tech
The updated Dict_1:  {'a': 0, 'e': 3, 'i': 0, 'o': 0, 'u': 0}
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions