1

I have started on a program to count vowels and have seemed to be getting nowhere. I need to count vowels from a string and then display the vowels. I need to do this by storing the number of occurrences in variables. Like this :

    a = 0
    b = 0
    ....

    then print the lowest.

Current code (its not that much ):

string = str(input("please input a string:  "))
edit= ''.join(string)


print(edit)

I have tried a number of methods just by my self and don't seem to get anywhere.

ThinkChaos
  • 1,803
  • 2
  • 13
  • 21
chaza194
  • 11
  • 1
  • 4

4 Answers4

3

You could use a dictionary comprehension:

>>> example = 'this is an example string'
>>> vowel_counts = {c: example.count(c) for c in 'aeoiu'}
>>> vowel_counts
{'i': 2, 'o': 0, 'e': 5, 'u': 0, 'a': 2}

Then finding the minimum, maximum etc. is trivial.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
2
>>> a="hello how are you"
>>> vowel_count = dict.fromkeys('aeiou',0)
>>> vowel_count 
{'a': 0, 'i': 0, 'e': 0, 'u': 0, 'o': 0}
>>> for x in 'aeiou':
...     vowel_count[x]=a.count(x)
... 
>>> vowel_count 
{'a': 1, 'i': 0, 'e': 2, 'u': 1, 'o': 3}

now from here you can print low nd max

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
0

You can use dictionary for this problem. Iterate over each character and if the character is a vowel, put it in dictionary with count 0 and increment its count by 1, and for every next occurrence keep incrementing the count.

>>> string = str(input("please input a string:  "))
please input a string:  'Hello how are you'
>>> dt={}   # initialize dictionary
>>> for i in string:  # iterate over each character
...    if i in ['a','e','i','o','u']:   # if vowel
...       dt.setdefault(i,0)  # at first occurrence set count to 0
...       dt[i]+=1   # increment count by 1
...
>>> dt
{'a': 1, 'u': 1, 'e': 2, 'o': 3}
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
0
word = input('Enter Your word : ')
vowel = 'aeiou'
vowel_counter = {}

for char in word:
  if char in vowel:
    vowel_counter[char] = vowel_counter.setdefault(char,0)+1

sorted_result = sorted(vowel_counter.items(), reverse=True,key=lambda x : x[1])

for key,val in sorted_result:
  print(key,val)
Fuji Komalan
  • 1,979
  • 16
  • 25