Exercise 21

Write a simple Python program that counts the number of vowels in a given string. Example: for the string s = 'anticonstitutionally' the program must return the following message: The string anconstitutionally has 9 vowels.

Solution

s = 'anticonstitutionally'
vowels = 'aeiou'
count = 0

for letter in s:
    if letter in vowels:
        count += 1

print(f"The string {s} has {count} vowels.")
"""
When you run this program with the string 'anticonstitutionally',
it will output:
The string anticonstitutionally has 9 vowels.
"""





In this program:

  1. we define the input string s and a string vowels: that contains all the vowel letters.
  2. We also initialize a variable count to 0: which will keep track of the number of vowels in the string.
  3. We then use the for loop: to iterate through each letter in the input string s, and check if the letter is a vowel by using the in operator to check if it's in the vowels string.
  4. If the letter is a vowel: we increment the count variable by 1.
  5. Finally: we print out a message using an f-string that displays the original string s and the number of vowels that were found in it.

 

Younes Derfoufi
CRMEF OUJDA

One thought on “Solution Exercise 21: algorithm python that determines the number of vowels in given string”

Leave a Reply