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.
I don’t understand. What’s your question?
– Leonel Sanches da Silva
I’m learning programming now, and I’ve chosen python and I can’t seem to solve this school exercise
– Edson Fonseca
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.
– Leonel Sanches da Silva
thank you very much
– Edson Fonseca