Python Program to Count the Number of Vowels in a String

1. Introduction

Vowels are an essential part of English phonetics, and counting them in a given string is a common task in text analysis. This simple Python program demonstrates how to count the number of vowels in a string.

In the English language, the letters A, E, I, O, U (and sometimes Y) are considered vowels. The task here is to iterate over the string and count occurrences of these characters.

2. Program Steps

1. Define a string that contains a sequence of characters.

2. Create a list or set of vowels for reference.

3. Initialize a count variable to zero.

4. Loop through the string, checking each character to see if it is a vowel.

5. Increment the count for each vowel encountered.

6. Print the total count of vowels.

3. Code Program

# Define the string
input_string = "Python Programming is fun."

# Define the set of vowels
vowels = set('aeiouAEIOU')

# Initialize the vowel count
vowel_count = 0

# Loop through the string and count the vowels
for char in input_string:
    if char in vowels:
        vowel_count += 1

# Print the vowel count
print(f"The number of vowels in the string is: {vowel_count}")

Output:

The number of vowels in the string is: 6

Explanation:

1. input_string is the string in which we want to count vowels.

2. vowels is a set containing all uppercase and lowercase vowels.

3. vowel_count is initialized to 0 and is used to track the number of vowels found.

4. The for loop iterates over each character char in input_string.

5. The if condition checks if the current char is present in the vowels set.

6. For every vowel found, vowel_count is incremented by 1.

7. After the loop completes, vowel_count reflects the total number of vowels in input_string.

8. The print statement outputs this count, showing that there are 6 vowels in the string "Python Programming is fun.".

Comments