You are trying to replace the string with the value 'i'
, not the contents of the variable i
.
Your code is also very inefficient; you don't need to loop over every character in text
; a loop over vowel
is enough. Because you already include both upper and lowercase versions, the two loops over the lowercased and uppercased versions are in essence checking for each vowel 4 times.
The following would be enough:
def anti_vowel(text):
text = str(text)
vowel = "aeiouAEIOU"
for i in vowel:
text = text.replace(i,' ')
return text
You are also replacing vowels with spaces, not just deleting them.
The fastest way to delete (rather than replace) all vowels would be to use str.translate()
:
def anti_vowel(text):
return text.translate(text.maketrans('', '', 'aeiouAEIOU'))
The str.maketrans()
static method produces a mapping that'll delete all characters named in the 3rd argument.