Can we use this code? answer is none


#1

def anti_vowel(text):
    for i in text:
        if i in "oiueaOIUEA":
            text.replace(i,"")
    return text

can we use this code?
answer is none


Anti_vowel
#2

Short answer, no. We cannot mutate an object when iterating over values. i cannot be changed, and neither can text.

As a general rule, strings should be treated as immutable. We can iterate over the string and store the valid characters in a list, though. This way we are not altering the string.

    x = []
    for i in text:
        if not i.lower() in "aeiou":
            x.append(i)
    return ''.join(x)

While we are bound to not change a string directly, we can create a filtered copy of it using the Python regular expression module.

@appylpye demonstrates it in this post:

import re
def anti_vowel(text):
    return re.sub("[aeiou]", "", text)

print anti_vowel("quick brown fox jumps over the lazy dog")

# qck brwn fx jmps vr th lzy dg

To ignore case,

import re
def anti_vowel(text):
    return re.sub("[aeiou]", "", text, flags=re.IGNORECASE)

print anti_vowel("Quick Brown Fox Jumps Over The Lazy Dog")

# Qck Brwn Fx Jmps vr Th Lzy Dg

#3

thanks a lot.....!!!