Why Does Every Letter in my String get Replaced Instead of Vowels?

Question:

I have the following code:

def replace_vowel(text):
    text = list(text)
    for i in range(0, len(text)):
        if text[i] == 'A' or 'a' or 'E' or 'e' or 'I' or 'i' or 'O' or 'o' or 'U' or 'u':
            text[i] = ''
    print ''.join(text)
    return ''.join(text)

replace_vowel("HeylookWords!")

The code is supposed to replace every vowel, but instead, every letter gets replaced with ”. Why does this happen?

Asked By: Well-Known Member

||

Answers:

Do range(len(text))

And you need == for each comparison. You can’t just do text[i] == ‘A’ then a bunch of or statements.

if text[i] == ‘A’ or text[i] == ‘a’ or… ect

Note that you can also do replace in python, 'some string'.replace('with something','for something else')

If you had a long list of checks you would put your letters into a list and check against the list.

l = ['A','B','C','D']
for letter in l:
   if text[i] == letter:
       do_something()
Answered By: Chad Crowe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.