Correct code to remove the vowels from a string in

2019-01-02 21:37发布

I'm pretty sure my code is correct but it doesn't seem to returning the expected output:

input anti_vowel("Hey look words") --> outputs: "Hey lk wrds".

Apparently it's not working on the 'e', can anyone explain why?

def anti_vowel(c):
    newstr = ""
    vowels = ('a', 'e', 'i', 'o', 'u')
    for x in c.lower():
        if x in vowels:
            newstr = c.replace(x, "")        
    return newstr

标签: python
13条回答
泛滥B
2楼-- · 2019-01-02 22:09

One more simpler way can be extracting the non-vowel characters from string and returning them.

def anti_vowel(text):
    newstring=""
    for i in text:
        if i not in "aeiouAEIOU":
            newstring=newstring+i
    text=newstring
    return text
查看更多
登录 后发表回答