Anti_vowel question, replace


#1

Hi, I am a bit confused as to why this removes the first vowel, only for it to return later. My code is below:
def anti_vowel(text):
----vowels = "aAeEiIoOuU"
----for i in text:
--------for j in vowels:
------------if i == j:
----------------txt = text.replace(i, "")
----------------print txt
----return txt

print anti_vowel("hello world!")

I get the below (minus the period, it censors h.ell)
hllo world!
h.ell wrld!
h.ell wrld!
h.ell wrld!
None

If I replace the code with text = text.replace("i", "") and return text instead, I will get the right answer "hll wrld".

def anti_vowel(text):
vowels = "aAeEiIoOuU"
for i in text:
    for j in vowels:
        if i == j:
            text = text.replace(i, "")
return text

Could anyone explain why the first code removes the e then adds it back and why the second code is different?

Thanks.


#2

does not update the text string, but is a shallow copy of it. text is unchanged as a result.

   text = text.replace(i,'')

is a shallow copy written back onto the text variable, so is updated.