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?

eightShirt
  • 1,457
  • 2
  • 15
  • 29
newbie
  • 2,412
  • 5
  • 22
  • 26
  • 2
    related: [What is the most efficient way in Python to convert a string to all lowercase stripping out all non-ascii alpha characters?](http://stackoverflow.com/questions/638893/what-is-the-most-efficient-way-in-python-to-convert-a-string-to-all-lowercase-str) – jfs Sep 04 '11 at 18:49

9 Answers9

11
re.sub('[aeiou]', '', 'Banana', flags=re.I)
JBernardo
  • 32,262
  • 10
  • 90
  • 115
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'))
agf
  • 171,228
  • 44
  • 289
  • 238
  • 2
    to speed it up use [bytes.translate()](http://stackoverflow.com/questions/7301292/string-replace-vowels-in-python/7301382#7301382). – jfs Sep 04 '11 at 19:00
  • @JFS +1 to your answer, that's the "best" way, but it doesn't help the OP understand basic loops, `in`, or string methods. – agf Sep 04 '11 at 19:27
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'
jfs
  • 399,953
  • 195
  • 994
  • 1,670
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)
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
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
Seven
  • 3,232
  • 1
  • 17
  • 15
  • it's been a while, but this is a method easy to understand for beginners. I recommed you understanding this. – InDaPond Jan 13 '18 at 17:17
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!

agf
  • 171,228
  • 44
  • 289
  • 238
  • 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 ;-) – Hod - Monica's Army Sep 04 '11 at 19:18
  • you should fix the indentation in the code example, nice and simple though – steabert Sep 04 '11 at 19:59
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
RedBaron
  • 4,717
  • 5
  • 41
  • 65
petr jandak
  • 11
  • 1
  • 1
0
def _filterVowels(word):
    for char in word:
        if char.lower() not in 'aeiou':
            yield char

def removeVowels(word):
    return "".join(_filterVowels(word))
Katriel
  • 120,462
  • 19
  • 136
  • 170
  • 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. – Katriel Sep 04 '11 at 18:36
0

how about this:

import re

def removeVowels(word):
    return re.sub("[aeiouAEIOU]", "", word)
Sébastien RoccaSerra
  • 16,731
  • 8
  • 50
  • 54