Count vowels and know how many there are

Asked

Viewed 11,453 times

3

Good! I wanted to create a function that would allow me to say if a word or phrase has vowels, and if so, how many vowels have, but I have 2 problems in my code that I don’t understand:

def conta_vogais(str):
    vogais = ['a','e','i','o','u']
    if str.lower() in vogais:
        return True
        vogais.count('a','e','i','o','u')
    else:
        return False

When running the program, it seems not to accept more than one letter to run if statement and qd put only one letter tb n tells me the value of the count. Can anyone help me?

2 answers

4

There is a class in Python called Counter. With it you can do it efficiently and without reinventing the wheel.

import re
from collections import Counter
def vogais(string):
    return Counter(re.sub('[^aeiouAEIOU]', '', string))
  • @Miguel O Counter accepts an iterable-or-Mapping and a string in Python is iterable. Type for c in 'teste': print(c).

  • No problem, @Miguel. This shows how often a "insightful" code, as I said, is not so good to be maintained. It gets messy all in one line and it’s not clear what’s going on.

  • @Miguel but there you would know the total of all vowels, not the amount of each. On the second comment, I totally agree. I edited the code.

3

You can do it like this:

1.

def conta_vogais(string):
    string = string.lower() # para que a comparação não seja sensível a maiuscula/minuscula
    result = {}
    vogais = 'aeiou'
    for i in vogais:
        if i in string:
            result[i] = string.count(i)
    return result

print(conta_vogais('olaaa'))

Or by making dictinary compreension suffice::

def conta_vogais(string):
    string = string.lower() # para que a comparação não seja sensível a maiuscula/minuscula
    vogais = 'aeiou'
    return {i: string.count(i) for i in vogais if i in string}

print(conta_vogais('olaaa'))

Output:

{'a': 3, 'o': 1}


2.

I did the above example in order to count and return the number of times it appears, and only if it appears. But if you simply want total vowels you can:

def conta_vogais(string):
    string = string.lower() # para que a comparação não seja sensível a maiuscula/minuscula
    result = 0
    vogais = 'aeiou'
    for i in vogais:
        result += string.count(i)
    return result

print(conta_vogais('olaaa'))

OR:

def conta_vogais(string):
    string = string.lower() # para que a comparação não seja sensível a maiuscula/minuscula
    vogais = 'aeiou'
    return sum(string.count(i) for i in vogais)

print(conta_vogais('olaaa'))

What will happen in this case 4


3.

By the way, to complete a little more the answer, you can also return the number of times that appears each (those that do not appear including):

def conta_vogais(string):
    string = string.lower() # para que a comparação não seja sensível a maiuscula/minuscula
    result = {}
    vogais = 'aeiou'
    for i in vogais:
        result[i] = string.count(i)
    return result

print(conta_vogais('olaaa'))

Or by making dictinary compreension suffice:

def conta_vogais(string):
    string = string.lower() # para que a comparação não seja sensível a maiuscula/minuscula
    vogais = 'aeiou'
    return {i: string.count(i) for i in vogais}

print(conta_vogais('olaaa'))

Output:

{'a': 3, 'e': 0, 'u': 0, 'i': 0, 'o': 1}

  • Thanks!! You helped me so much :)

  • Welcome to stack overflow EN @Miguelluís

Browser other questions tagged

You are not signed in. Login or sign up in order to post.