3

Hi is there a better way of writing this code instead of writing the repetitive multiple if statements? I'm trying to find a better way of writing this code. Basically I want to count up the the total number of matching letters in the given string s.

s = 'abcdbobbobbegkhl'
count = 0
for letter in s:
    if letter == 'a':
        count += 1
    if letter == 'e':
        count += 1
    if letter == 'i':
        count += 1
    if letter == 'o':
        count += 1
    if letter == 'u':
        count += 1
print('Number of vowels: ' + str(count))
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
Tim
  • 47
  • 4

7 Answers7

9

You can also use a list comprehension:

s = 'abcdbobbobbegkhl'
count = len([i for i in s if i in 'aeiou'])
print('Number of vowels: ' + str(count))
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
4
s = 'abcdbobbobbegkhl'
count = 0
for letter in s:
    if letter in 'aeiou':
        count += 1
print('Number of vowels: ' + str(count))

Or a one-liner:

count = sum(l in 'aeiou' for l in s)
theEpsilon
  • 1,800
  • 17
  • 30
  • Is there a way that I can use the == operator for this? – Tim Jun 12 '20 at 18:36
  • @Tim yes you could use the `or` keyword – theEpsilon Jun 12 '20 at 18:40
  • I guess I'm having diffculty wrapping my head around the concept or understanding of a if statement inside a for loop in this example. Can you explain this whole idea of this algorithm in detail? For example if we already establish the for loop: for letter in s, then why is it follow by the statement: if letter in ['a', 'e', 'i', 'o', 'u']? Can't we establish another variable outside of the for loop, let's call it vowel = 'aeiou', follow by a for loop: for letter in s, then inside the for loop set vowel == s, then increment with a counter count += 1. Is this logic right? – Tim Jun 12 '20 at 20:43
  • How would I write this code if I want to use the operator == ? – Tim Jun 12 '20 at 20:51
  • @Tim we go through every letter that is in `s`. Then we test it to see if it is also inside the string 'aeiou'. In other words, `if letter == 'a' or letter == 'e' or letter == 'i' or letter =='o' or letter == 'u'`. Does that make sense? – theEpsilon Jun 12 '20 at 21:19
  • yes that makes sense now. Thank you for clarifying the concepts for me mate! – Tim Jun 12 '20 at 23:16
1
s = 'abcdbobbobbegkhl'
count = 0
vowels = ['a','e','i','o','u']

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

print('Number of vowels: ' + str(count))
Sean Ziegler
  • 123
  • 5
  • So in this example is it correct to think of the if statement inside of this for-loop, as " iterate through all letters in the variable string s", then inside the string s look for or match the letters strings in s with the letter string in variable vowel? – Tim Jun 12 '20 at 20:49
  • The for loop iterates over every character in the string ```s``` using a variable ```letter```. The if statement checks each ```letter``` to see if it is contained in the list ```vowels``` and increments the ```count``` if it is a vowel. Does that help? – Sean Ziegler Jun 12 '20 at 23:56
  • Try giving this a read: [Python Iterators](https://www.programiz.com/python-programming/iterator) – Sean Ziegler Jun 13 '20 at 00:01
1

Can be written in many ways. One of them is like :

for letter in s:
    if letter in ['a','e','i','o','u']:
        count = count+1
print('Number of vowels: ' + str(count))
  • I guess I'm having diffculty wrapping my head around the concept or understanding of a if statement inside a for loop in this example. Can you explain this whole idea of this algorithm in detail? For example if we already establish the for loop: for letter in s, then why is it follow by the statement: if letter in ['a', 'e', 'i', 'o', 'u']? Can't we establish another variable outside of the for loop, let's call it vowel = 'aeiou', follow by a for loop: for letter in s, then inside the for loop set vowel == s, then increment with a counter count += 1. Is this logic right? – Tim Jun 12 '20 at 20:43
0

Here is another solution, which may be nice:

s = 'abcdbobbobbegkhl'

vowels = 'aeiou'
count= 0

for vowel in vowels:
    count += s.count(vowel)

print('Number of vowels: ' + str(count))
Roland Deschain
  • 2,211
  • 19
  • 50
0

Using a dict as the vocals set, you made direct access to vocals instead of to find in a list.

vocals = {'a':1,'e':1,'i':1,'o':1,'u':1}
s = "aeioutr"
count = 0
for letter in s:
  try:
    count+=vocals[letter]
  except KeyError:
    print(f"{letter} is not a vocal")
print(count) #5
Angelo Mendes
  • 905
  • 13
  • 24
0

OK, not exactly what was asked for, but might be useful anyway if the OP's goal is to count letter frequencies. This is using Counter to keep track of letter counts.

from collections import Counter

s = "I am a foobar and I have many vowels"

vowels = "aeiou"

vowels += vowels.upper()

counter = Counter(s)

sum_ = 0
for ch in vowels:
    sum_ += counter[ch]

print(f"Number of vowels:{sum_}")

Output:

Number of vowels:13
JL Peyret
  • 10,917
  • 2
  • 54
  • 73