7

Expected:

>>> removeVowels('apple')
"ppl"
>>> removeVowels('Apple')
"ppl"
>>> removeVowels('Banana')
'Bnn'

Code (Beginner):

def removeVowels(word):
    vowels = ('a', 'e', 'i', 'o', 'u')
    for c in word:
        if c in vowels:
            res = word.replace(c,"")
    return res 

How do I both lowercase and uppercase?

1

9 Answers 9

11
re.sub('[aeiou]', '', 'Banana', flags=re.I)
Sign up to request clarification or add additional context in comments.

1 Comment

or vowels = re.compile('[aeiou]', flags=re.I) and then vowels.sub('','Banana')
9

Here is a version using a list instead of a generator:

def removeVowels(word):
    letters = []            # make an empty list to hold the non-vowels
    for char in word:       # for each character in the word
        if char.lower() not in 'aeiou':    # if the letter is not a vowel
            letters.append(char)           # add it to the list of non-vowels
    return ''.join(letters) # join the list of non-vowels together into a string

You could also write it just as

''.join(char for char in word if char.lower() not in 'aeiou')

Which does the same thing, except finding the non-vowels one at a time as join needs them to make the new string, instead of adding them to a list then joining them at the end.

If you wanted to speed it up, making the string of values a set makes looking up each character in them faster, and having the upper case letters too means you don't have to convert each character to lowercase.

''.join(char for char in word if char not in set('aeiouAEIOU'))

2 Comments

to speed it up use bytes.translate().
@JFS +1 to your answer, that's the "best" way, but it doesn't help the OP understand basic loops, in, or string methods.
6

Using bytes.translate() method:

def removeVowels(word, vowels=b'aeiouAEIOU'):
    return word.translate(None, vowels)

Example:

>>> removeVowels('apple')
'ppl'
>>> removeVowels('Apple')
'ppl'
>>> removeVowels('Banana')
'Bnn'

Comments

3

@katrielalex solution can be also simplified into a generator expression:

def removeVowels(word):
    VOWELS = ('a', 'e', 'i', 'o', 'u')
    return ''.join(X for X in word if X.lower() not in VOWELS)

Comments

2

Since all the other answers completely rewrite the code I figured you'd like one with your code, only slightly modified. Also, I kept it simple, since you're a beginner.

A side note: because you reassign res to word in every for loop, you'll only get the last vowel replaced. Instead replace the vowels directly in word

def removeVowels(word):
    vowels = ('a', 'e', 'i', 'o', 'u')
    for c in word:
        if c.lower() in vowels:
            word = word.replace(c,"")
    return word

Explanation: I just changed if c in vowels: to if c.lower() in vowels:

.lower() convert a string to lowercase. So it converted each letter to lowercase, then checked the letter to see if it was in the tuple of vowels, and then replaced it if it was.

The other answers are all good ones, so you should check out the methods they use if you don't know them yet.

Hope this helps!

2 Comments

Glad to help. as other people have pointed out though, if you extend your tuple to include capital vowels your code will run faster than having to convert each letter to lowercase. I just tried to stick to your general format and stay simple. Accept this answer if you like it ;-)
you should fix the indentation in the code example, nice and simple though
2

Here you have another simple and easy to use function, no need to use lists:

def removeVowels(word):
    vowels = 'aeiouAEIOU'
    for vowel in vowels:
        word = word.replace(vowel, '')
    return word

1 Comment

it's been a while, but this is a method easy to understand for beginners. I recommed you understanding this.
0
def _filterVowels(word):
    for char in word:
        if char.lower() not in 'aeiou':
            yield char

def removeVowels(word):
    return "".join(_filterVowels(word))

1 Comment

I can explain it, but I think it will do you good to read up on the docs and see if you can figure it out for yourself! It's a really really good tool to have in your arsenal when learning Python.
0

how about this:

import re

def removeVowels(word):
    return re.sub("[aeiouAEIOU]", "", word)

Comments

0
def removeVowels(word):
    vowels='aeiou'
    Vowels='AEIOU'
    newWord=''

    for letter in word:
        if letter in vowels or letter in Vowels:
            cap=letter.replace(letter,'')
        else: cap=letter
        newWord+=cap
    return newWord

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.