Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Disemvowel

Plz tell how to do this

Plz send me the correct code to solve this problem

disemvowel.py
def disemvowel(word):
    return word
vowel = ["a" , "i", "e","o","u"]
try:
    word.remove(vowel)
except ValueError:
    pass

1 Answer

Hello Ashmit,

You seem a little lost.

  • All of the code you write should be within the disemvowel function.
  • You cannot remove vowel from word, as word does not contain a list (it's a string!).
  • You do not need a try/except block for this problem.

Unfortunately, as I have stated, you cannot remove an array from a string. It simply doesn't make sense. Moreover, even if you remove a single character from a string, it only would remove the first find for that character, it wouldn't remove all finds.

This means you must iterate through the string, and if the character you're on isn't in the string "aeiou", then you may append the character to the result string which you will return.

Here's the code, but please do not copy and paste the code, just understand how it works. Trust me, if you type it out on your own, you will learn programming much better.

def disemvowel(word):
    result = ''
    for i in word:
        if i not in "aeiouAEIOU":
            result += i
    return result

Happy coding! :zap: ~Alex

UPDATED DUE TO MISTAKE IN CODE

sir why have to take this step result += i in the code plz explain

result += i appends i to the end of the string result.

For example...

(Pretend I'm in the Python Shell.)

>>> foobar = 'foo'
>>> foobar
'foo'
>>> foobar += 'bar'
>>> foobar
'foobar'
>>>

It's actually a shorthand for this:

result = result + i

And, as you may know, result and i are both strings, and in Python, when you add two strings together, it "glues" them together.

I hope this helps :smile: