Here is another interesting Python program that will take a word or sentence as an input (basically a string) and return the number of vowels present in it.
There is numerous way you can write a Python program to count the number of vowels in a string, but here I am going to write as much as easily as possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#Python script to find vowels in a string print "Enter a string:" #define i i = 0; #get the input string inpstr = raw_input() #convert string to lower case inplower = inpstr.lower() vowels = ['a','e','i','o','u'] #loop through each character for c in inplower: #if vowel found then increment i if c in vowels: i = i + 1 #print output print "Number of vowels: "+ str(i) |
Read the inline comments in the above script to understand it.
Output:
1 2 3 |
Enter a string: agurchand Number of vowels: 3 |
Enjoy coding!