How to print all vowels present in string?

Asked

Viewed 1,662 times

0

I’m learning to program now, and I’ve chosen Python, and I’m having trouble solving this school exercise. The code that I have is:

nome = input("Digite o nome: ")
b = nome.find("a")

This code finds 15 for nome equal to "one two three four fourteen", but wanted to find all letters a and their positions.

  • I don’t understand. What’s your question?

  • I’m learning programming now, and I’ve chosen python and I can’t seem to solve this school exercise

  • But you have some code ready, started? Here the site is not for us to write the code for you. Here you asked a question about, for example, Python, and we answered. I will edit your question to help you.

  • 1

    thank you very much

1 answer

3


str.find

The idea you used is very close to the final solution, the main problem is that the method find of string returns only the index of the first occurrence of the search letter.

str.find(sub[, start[, end]])

Return the lowest index in the string Where substring sub is found Within the Slice s[start:end]. Optional Arguments start and end are Interpreted as in Slice Notation. Return -1 if sub is not found.

To find all occurrences, we could use the parameter start, that defines where will begin the analysis within the string. If we define this value as the first position of the letter, the method returns the position of the second occurrence.

nome = input("Digite o nome: ")

start = 0

while True:
    index = nome.find("a", start)
    
    if index == -1:
        break
    
    print("Letra 'a' encontrada na posição %d" % index)
    start = index+1

For an entrance equal to batata, the exit would be:

Letra 'a' encontrada na posição 1
Letra 'a' encontrada na posição 3
Letra 'a' encontrada na posição 5

For in positions 1, 3 and 5 there is the letter "a".

If we wanted to do the same process for the other vowels, we would have to repeat the code. Still, this way differentiates the letters "a" and "a", so for the name Anderson, the letter "a" would not be found.

loop

Another very simple method, easy to understand for beginners, is to iterate on the string through a loop and check if the letter is a vowel. For example:

nome = input("Digite o nome: ")

for index, letra in enumerate(nome):
    if letra == 'a' or \
       letra == 'e' or \
       letra == 'i' or \
       letra == 'u':
        
        print("Encontrado a letra '%c' na posição %d" % (letra, index))

For the name batata the exit will be:

Encontrado a letra 'a' na posição 1
Encontrado a letra 'a' na posição 3
Encontrado a letra 'a' na posição 5

This method can still be optimized for:

nome = input("Digite o nome: ")

for index, letra in enumerate(nome):
    if letra in "aeiou":
        print("Encontrado a letra '%c' na posição %d" % (letra, index))

This way it would be easier to get around the problem of lowercase or uppercase letters, as it would be enough to do:

nome = input("Digite o nome: ")

for index, letra in enumerate(nome):
    if letra in "aeiouAEIOU":
        print("Encontrado a letra '%c' na posição %d" % (letra, index))

That the program would work for any vowel, both lowercase and uppercase.

re.findall

A slightly more advanced solution is using regular expressions. Python has a native library called re having a function called findall, which returns the list of all occurrences of a pattern in a string. For example, the following code:

import re

nome = input("Digite o nome: ")

print(re.findall(r"[aeiou]", nome, re.IGNORECASE))

Returns the list of all vowels found in the name. To batata, would be returned ['a', 'a', 'a'], referring to the three "a" present in the word.

  • thank you very much, overcame my difficulty and taught me

Browser other questions tagged

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