I want to make a function that returns the text that the user has entered but without any vowel, it doesn't matter if it was written with uppercase or lowercase.
Then as an extra, I would like to invest it but I think that is easier.
This is my code:
def invertirSinVocales():
vocales = ("a", "e", "i", "o", "u", "A", "E", "I", "O","U")
texto = (input("ingrese un texto: "))
while vocales in texto:
texto2= texto.replace(vocales,"")
return texto2
print(invertirSinVocales())
The problem is that it tells me that it expects "vowels" to be a str and it is actually a tuple.
Hello try this to see if it is what you need:
A couple of more "gimmicky" options:
Use
str.traslate
:str.maketrans
allows you to create a table that will be used to replace each character with the one that corresponds to it. Receive a dictionary where the pairsclave: valor
are:since in this case we want to eliminate them, it is enough that it be of the form:
which is what you create
dict.fromkeys('aeiouAEIOUüáéíóúÜÁÉÍÓÚ')
. For its partstr.maketrans
, it returns a similar dictionary but converting the keys into numerical values (unicode codepoints of each character).str.translate
it simply loops through the string and uses the table to change each character to the one it belongs to, returning the new string on completion.Using regular expressions with
re.sub
:`This doesn't require much explanation, it searches the string for each match of the pattern and replaces it with the second argument (an empty string in this case). The flag
re.IGNORECASE
makes it case-insensitive when looking for pattern matches.you can use "List comprehensions" like this (updated with @FJsevilla's suggestions):
I hope it helps you and if you have any questions, you can comment on it.
Explanation in Spanish:
"".join()
: Join the elements of the list separating them with the character""
[nv for nv in texto if nv not in vocales]
: add to the list each character (nv
) in the stringtexto
ifnv
it is not found in the listvocales
.This, written as a
for
traditional would be:Hello, you can do something like this:
Result:
Explanation: The removeVocales function receives a word as a parameter , then through a for loop we go through said word and through a conditional if we verify if there is any vowel after conversion to lowercase (.lower).
I hope it works for you and good luck!
This is a very simple way: