Write a function that counts the vowels (aeiou) in a string the user inputs. Make sure it counts upper- and lowercase vowels. Then write a routine that calls the function and displays the following output.
$ ./count_vowels.py
Enter some words: Go East young man!
The string "Go East young man!" has 6 vowels in it.
$ cat count_vowels.py
#!/usr/bin/python
def countVowels(my_string):
count = 0
for s in my_string:
if s.lower() in 'aeiou':
count += 1
return count
stg = raw_input('Enter some words: ')
print 'The string \"' + stg + '\"' + ' has ',
print countVowels(stg),
print 'vowels in it.'
Following is an alternative function that performs the same task:
$ cat count_vowels2.py
#!/usr/bin/python
def countVowels(my_string):
my_string = my_string.lower()
return len(my_string) - len(my_string.translate(None,'aeiou'))
stg = raw_input('Enter some words: ')
print 'The string \"' + stg + '\"' + ' has ',
print countVowels(stg),
print 'vowels in it.'
You might also like to view...
To allow revisions to cells in a protected worksheet, you must first ________ the cells
Fill in the blank(s) with the appropriate word(s).
The Windows Start screen displays thumbnails of Office sample documents
Indicate whether the statement is true or false.