Welcome to the Treehouse Community
Looking to learn something new?
Help with this for loop.
def disemvowel(word): vowels = 'aeiouAEIOU' result = '' for letter in word: if letter in vowels: result = word.replace(letter, '') return result
I'm losing my sanity with this one?
it will proof when passed 'here' but when passed something like 'placeit' , no good?
can anyone explain this to me?
def disemvowel(word): vowels = 'aeiou' result = '' for letter in word: if letter in vowels: result = word.replace(letter, '') return result
Maurice AbneyCourses Plus Student 9,847 Points
The trick to this solution lies in the fact that strings are immutable(cant change) but lists are mutable(ie, do work on list and then convert to string). What's being returned from each of your replace() calls is a new string acting on the original word, its not updated at all, so your end result will simply be the original word with the 'a's removed, e's i's o's and u's intact. reason being, after the first replace, the result is returned. A similar solution could be done with recursion by calling returning disemvowel(result) and changing a few lines but that's another discussion. You dont need recursion to solve this problem.
Here's my solution:
def disemvowel(word): result =  # empty list for letter in word: if letter.lower() not in "aeiou": result.append(letter) return ''.join(result) #join list back into string