Python Program To Calculate How Many Times Every Vowel Has Been Repeated in A Word

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Python program

to calculate how
many times every
vowel has been
repeated in a
word
Code:
word = input("Enter a word: ")

# Initialize counters for each vowel


a_count = 0
e_count = 0
i_count = 0
o_count = 0
u_count = 0

# Loop through each character in the


word
for char in word:
# If the character is a vowel, increment
the corresponding counter
if char in "aeiouAEIOU":
if char.lower() == 'a':
a_count += 1
elif char.lower() == 'e':
e_count += 1
elif char.lower() == 'i':
i_count += 1
elif char.lower() == 'o':
o_count += 1
elif char.lower() == 'u':
u_count += 1

# Print the results


print("The word '{}'
contains:".format(word))
print("- {} 'a' vowels".format(a_count))
print("- {} 'e' vowels".format(e_count))
print("- {} 'i' vowels".format(i_count))
print("- {} 'o' vowels".format(o_count))
print("- {} 'u' vowels".format(u_count))
Explanation of the code

This Python code asks the user to input a word


and then count the number of vowels in the
word. It does so by initializing a counter for
each vowel (a, e, i, o, u) to 0, and then looping
through each character in the word. If the
character is a vowel, the corresponding counter
is incremented. Finally, the program prints out
the results. This Python code tells that prompts
the user to enter a word and then counts the
number of vowels in the word.
This Python code takes an input word from the
user, counts the number of vowels in the word,
and prints out the counts for each vowel
separately. First, the code prompts the user to
enter a word using the input() function and
stores the word in the variable 'word'. Then, the
code initializes a counter variable for each
vowel 'a', 'e', 'i', 'o', and 'u' to zero. Next, a for
loop is used to iterate over each character in the
input word. For each character, the code checks
if it is a vowel using an if statement that checks
if the character is present in the string
"aeiouAEIOU" . If the character is a vowel, the
corresponding counter variable is incremented
by one. After the for loop has finished iterating
through each character in the word, the code
prints out the results. It uses the format()
function to insert the values of the vowel
counters into a string that describes the counts
for each vowel and then uses the print() function
to display the results on the screen. Overall, the
code is a simple example of how to count the
occurrence of certain characters in a string in
Python. This code allows a user to input a word
and then count the number of vowels in that
word, displaying the count for each vowel type
at the end. Here's how the code works: The first
line prompts the user to enter a word using the
input() function and assigns the inputted word to
the variable word. Next, variables are initialized
for each vowel count to 0 using the syntax
variable name = 0. These variables will be used
to keep track of the count of each vowel type in
the inputted word. Then, a for loop is used to
iterate over each character in the word variable.
The loop variable char represents each character
in the word. The loop contains an if statement
that checks whether the character is a vowel. If
it is, the corresponding counter variable is
incremented by 1 using the syntax variable
name += 1. The if statement also checks
whether the character is uppercase or lowercase,
so the code can count both uppercase and
lowercase vowels correctly. The lower()
function is used to convert any uppercase
vowels to lowercase before comparing them to
the if statement's conditions. Finally, the code
prints out the results of the vowel counts using
the print() function. The curly braces {} are
used as placeholders for the values of the vowel
count variables, which are inserted into the
string using the format() method. The output
will show the original word inputted by the user,
and then a count of how many of each vowel
type are in that word.

You might also like