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. By IncludeHelp Last updated : February 12, 2024

Problem statement

Given a string, and we have to count the total number of vowels in the string using python program.

Example

The following is the sample input and output:

Input:
Str = "Hello world"

Output:
Total vowels are: 3

Python - Counting vowels in a string

To count the total number of vowels in a string, use for ... in loop to extract characters from the string, check whether the character is a vowel or not, and if the character is a vowel, then increase the counter.

Note

To check vowel alphabets, check the characters with uppercase and lowercase vowels.

Python program to count vowels in a string

The below example counts the total number of vowels in the given string.

# 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

The output of the above program is:

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.

Python program to count the number of vowels in a string

The below example counts the total number of vowels in the given string using the user-defined functions:

# 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

The output of the above program is:

Total vowels are:  3

To understand the above examples, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.