I'm suppose remove all the vowels from the any string that would be entered. I'm trying to make the code as simple as possible.
Thank you for the help.
def anti_vowel(text):
for i in text:
i.strip(['i','o','a','u','e'])
return i
So you call strip on each character.... and then what? You don't update the string, because strings are immutable and i.strip is not an inplace operation.
A naive improvement over your solution would filtering characters out inside a list comprehension and then doing a join on the result:
vowels = {'i','o','a','u','e'}
def anti_vowel(text):
return ''.join([c for c in text if c not in vowels])
A small note: if your string contains mixed case, you may want to either
text, orvowels to contain uppercase vowels: vowels = set('aeiouAEIOU'), orUse str.casefold (as per @Adam Smith's comment)—augmenting vowels is no longer needed in that case:
return ''.join([c for c in text if c.casefold() not in vowels])
You can get even better with str.translate (this works on python-3.x):
mapping = str.maketrans(dict.fromkeys(vowels, '')) # create a global mapping once
def anti_vowel(text):
return text.translate(mapping))
Two problems:
strip is the wrong method; it only removes from the beginning and end of a string. Use .replace(something, '').strip returns the modified string.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With