Exercise 21
Write a Python program to count the number of vowels in a given string. Example for the string s='unconstitutionally' the program must return the following message: "The string unconstitutionally has 8 vowels".
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# define vowels in a set vowels= {'a','e','y','u','i','o'} # define a string s = "unconstitutionally" # get the length of the string s n = len(s) # initialize number of vowels to 0 number_vowels = 0 # iterate through the characters of the string s for i in range(0,n): if(s[i] in vowels): number_vowels = number_vowels + 1 print("The number of vowels in the string 's' is: ", number_vowels) # output: The number of vowels in the string 's' is: 8 |
Younes Derfoufi
CRMEF OUJDA
1 thought on “Solution Exercise 21: number of vowels in a Python string”