Different and more efficient way of coding anti_vowel



My code behaves correctly. I’m just wondering of a more efficient way to code it. Loops for example.

def anti_vowel(text):
    text = text.replace('A', '')
    text = text.replace('a', '')
    text = text.replace('e', '')
    text = text.replace('E', '')
    text = text.replace('i', '')
    text = text.replace('I', '')
    text = text.replace('o', '')
    text = text.replace('O', '')
    text = text.replace('U', '')
    text = text.replace('u', '')
    return text
print anti_vowel("Test") 


There’s a few ways to do it. Here’s one way I have solved it.

You could easily do it without creating a another function like I did below, but I found it a bit more efficient to do it this way because I can change the text I’m looking at in the is_vowel function to lower-case and then only have 5 if statements instead of having if statements for upper-case letters, too.

48 PM


Hi, this is the way I did it.
I am always trying to minimize the number of lines as much as possible. But i dont know if it’s the most efficient way In terms of performance. :slight_smile:

def anti_vowel(text):
    text_lower = text.lower()
    new_word = ""
    for x in range(len(text)):
        if not(text_lower[x] == 'a' or text_lower[x] == 'e' or text_lower[x] == 'i' or text_lower[x] == 'o' or text_lower[x] == 'u'):
            new_word += text[x]
    return new_word


You can use a loop and make it shorter, just check the hint :wink:

it gives something like this:

def anti_vowel(text):
    txt = ""
    for i in text:
        if i not in "aeiouAEIOU":
            txt = txt+i
    return txt


I hate checking hints , it feels like i didnt do it then… :smiley: Heh, i think i’ll start doing that ,thanks :slight_smile:


I was answering the post author, who feel ok with asking on the forum, so i presume it’s ok to check the hint too, when you solved the problem your own way. :wink:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.