user1679073
user1679073

Reputation: 47

Count vowels from raw input

I have a homework question which asks to read a string through raw input and count how many vowels are in the string. This is what I have so far but I have encountered a problem:

def vowels():
    vowels = ["a","e","i","o","u"]
    count = 0
    string = raw_input ("Enter a string: ")
    for i in range(0, len(string)):
        if string[i] == vowels[i]:
            count = count+1
    print count

vowels()

It counts the vowels fine, but due to if string[i] == vowels[i]:, it will only count one vowel once as i keeps increasing in the range. How can I change this code to check the inputted string for vowels without encountering this problem?

Upvotes: 3

Views: 14138

Answers (6)

Rolf of Saxony
Rolf of Saxony

Reputation: 22453

Option on a theme

Mystring = "The lazy DOG jumped Over"
Usestring = ""
count=0
for i in Mystring:
    if i.lower() in 'aeiou':
        count +=1
        Usestring +='^'
    else:
        Usestring +=' '

print (Mystring+'\n'+Usestring)
print ('Vowels =',count)

The lazy DOG jumped Over
  ^  ^    ^   ^  ^  ^ ^ 
Vowels = 7

Upvotes: 0

Amber
Amber

Reputation: 527023

in operator

You probably want to use the in operator instead of the == operator - the in operator lets you check to see if a particular item is in a sequence/set.

1 in [1,2,3] # True
1 in [2,3,4] # False
'a' in ['a','e','i','o','u'] # True
'a' in 'aeiou' # Also True

Some other comments:

Sets

The in operator is most efficient when used with a set, which is a data type specifically designed to be quick for "is item X part of this set of items" kind of operations.*

vowels = set(['a','e','i','o','u'])

*dicts are also efficient with in, which checks to see if a key exists in the dict.

Iterating on strings

A string is a sequence type in Python, which means that you don't need to go to all of the effort of getting the length and then using indices - you can just iterate over the string and you'll get each character in turn:

E.g.:

for character in my_string:
    if character in vowels:
        # ...

Initializing a set with a string

Above, you may have noticed that creating a set with pre-set values (at least in Python 2.x) involves using a list. This is because the set() type constructor takes a sequence of items. You may also notice that in the previous section, I mentioned that strings are sequences in Python - sequences of characters.

What this means is that if you want a set of characters, you can actually just pass a string of those characters to the set() constructor - you don't need to have a list one single-character strings. In other words, the following two lines are equivalent:

set_from_string = set('aeiou')
set_from_list = set(['a','e','i','o','u'])

Neat, huh? :) Do note, however, that this can also bite you if you're trying to make a set of strings, rather than a set of characters. For instance, the following two lines are not the same:

set_with_one_string = set(['cat'])
set_with_three_characters = set('cat')

The former is a set with one element:

'cat' in set_with_one_string # True
'c' in set_with_one_string # False

Whereas the latter is a set with three elements (each one a character):

'c' in set_with_three_characters` # True
'cat' in set_with_three_characters # False

Case sensitivity

Comparing characters is case sensitive. 'a' == 'A' is False, as is 'A' in 'aeiou'. To get around this, you can transform your input to match the case of what you're comparing against:

lowercase_string = input_string.lower()

Upvotes: 9

lvc
lvc

Reputation: 35089

for i in range(0, len(string)):
    if string[i] == vowels[i]:

This actually has a subtler problem than only counting each vowel once - it actually only tests if the first letter of the string is exactly a, if the second is exactly e and so on.. until you get past the fifth. It will try to test string[5] == vowels[5] - which gives an error.

You don't want to use i to look into vowels, you want a nested loop with a second index that will make sense for vowels - eg,

for i in range(len(string)):
   for j in range(len(vowels)):
       if string[i] == vowels[j]:
          count += 1

This can be simplified further by realising that, in Python, you very rarely want to iterate over the indexes into a sequence - the for loop knows how to iterate over everything that you can do string[0], string[1] and so on, giving:

for s in string:
   for v in vowels:
      if s == v:
        count += 1

The inner loop can be simplified using the in operation on lists - it does exactly the same thing as this code, but it keeps your code's logic at a higher level (what you want to do vs. how to do it):

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

Now, it turns out that Python lets do math with booleans (which is what s in vowels gives you) and ints - True behaves as 1, False as 0, so True + True + False is 2. This leads to a one liner using a generator expression and sum:

sum(s in vowels for s in string)

Which reads as 'for every character in string, count how many are in vowels'.

Upvotes: 2

Mu Mind
Mu Mind

Reputation: 11204

Here's a more condensed version using sum with a generator:

def vowels():
    string = raw_input("Enter a string: ")
    print sum(1 for x in string if x.lower() in 'aeiou')

vowels()

Upvotes: 0

squiguy
squiguy

Reputation: 33380

You can simplify this code:

def vowels():
    vowels = 'aeiou'
    count = 0
    string = raw_input ("Enter a string: ")
    for i in string:
        if i in vowels:
            count += 1
    print count

Strings are iterable in Python.

Upvotes: 5

Joran Beasley
Joran Beasley

Reputation: 114028

you can use filter for a one liner

print len(filter(lambda ch:ch.lower() in "aeiou","This is a String"))

Upvotes: 1

Related Questions