It is easiest to use regular expressions for this problem. You can separate multiple characters by "|" and use the re.sub(chars_to_replace, string_to_replace_with, str). We have For example:
>>> import re >>> consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'] >>> re.sub('|'.join(consonants), "", "Hello people", flags=re.IGNORECASE) "eo eoe"
Note: You can also use the [] to create group of characters to replace in regex.
If you want to keep only vowels and remove all other characters, you can use an easier version. Note that it'll remove spaces, numbers, etc. as well. For example,
>>> import re >>> re.sub('[^aeiou]', "", "Hello people", flags=re.IGNORECASE) "eoeoe"
You can also filter out the consonants as follows:
>>> consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'] >>> s = "Hello people" >>> ''.join(c for c in s if c.lower() not in consonants) 'eo eoe'