Correct code to remove the vowels from a string in Python

python

Solution

The function `str.replace(old, new[, max])` don't changes `c` string itself (wrt to `c` you calls) just returns a new string which the occurrences of old have been replaced with new. So `newstr` just contains a string replaced by last vowel in `c` string that is the `o` and hence you are getting `"Hey lk wrds"` that is same as `"Hey look words".replace('o', '')`.

I think you can simply write `anti_vowel(c)` as:

''.join([l for l in c if l not in vowels]);

What I am doing is iterating over string and if a letter is not a vowel then only include it into list(filters). After filtering I join back list as a string.

Problem

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 ```

Original source

Related problems