hell0jack
hell0jack

Reputation: 55

Why is my function partially doing what it’s supposed to do?

So I’m trying to write a Python 3 function for a challenge that takes in a string, removes the vowels and returns it without the vowels. I wrote the below code but it seems to only take out the vowels partially while leaving some untouched.

def remove_vowels(string):
    vowels = ['a','e','i','o','u']
    newstring = ""

    for letter in string:
        if letter in vowels:
            newstring = string.replace(letter,””)
        else:
             pass

    return newstring

Upvotes: 5

Views: 146

Answers (6)

xkcdjerry
xkcdjerry

Reputation: 983

Your code has several defects:

  • You are updating new_string with string, which does not change, and that results in only the last vowel in the list is deleted.
  • You do not need else: pass, it's ok with only the if clause. The else: pass is redundant.
  • You are cycling through the letters in string, which is not the fastest way to do things, also, it will create all kinds of weird errors if your code is changing string. You can knock outfor i in new_string:if i in vowels with for i in vowels
  • The vowels does not have to be a list if you are only iterating over it.Try a tuple or a str for better efficiency.

The overall fix:

def remove_vowels(string):
    vowels = ('a','e','i','o','u')
    newstring = string
    for letter in vowels:
        newstring = newstring.replace(letter,””)
    return newstring

or:

def remove_vowels(string):
    vowels = ('a','e','i','o','u')
    for letter in vowels:
        string = string.replace(letter,””)
    return string

Upvotes: 0

pitamer
pitamer

Reputation: 1064

An alternative, short solution with a list comprehension:

def remove_vowels(string):
    not_vowels = [letter for letter in string if letter not in 'aeiou']
    return ''.join(not_vowels)

Upvotes: 2

Oddity
Oddity

Reputation: 480

Your original python code with the following input

print(remove_vowels("The quick brown fox jumps over the lazy dog"))

returns "The quick brwn fx jumps ver the lazy dg"

The reason this only removes the "o" vowel is because your iterating through each vowel and updating your new string to the passed string minus the current vowel your on. So for example the first time through your for loop your "newstring" variable will be:

The quick brown fox jumps over the lzy dog

Then the next iteration your "newstring" variable will be set to

Th quick brown fox jumps ovr th lazy dog

And so on and so on. The reason my example only removes the o's is because there is no U to replace so the string replace method is never called leaving the "newstring" variables without o's.

To fix this you would just remove newstring completely and update the original string as this is it's own variable and this would technically work as is.

Although this can be compressed and refactored for better performance quite easily as you don't actually need to iterate through your string at all (nor do you need any type of new string as the passed string is it's own variable you are able to update) as pythons "String.replace" will replace ALL occurrences of the supplied substring and return the resulting string (if you don't supply a max amount of occurrences).

The following code below works

def remove_vowels(string):
    vowels = 'aeiou'
    for vowel in vowels: string = string.replace(vowel,'')
    return string

print(remove_vowels("The quick brown fox jumps over the lazy dog"))

and returns "Th qck brwn fx jmps vr th lzy dg"

Upvotes: 6

newbee7
newbee7

Reputation: 1

You are replacing a character in original string each time, but the original string never changes with current logic (except in the last loop).

try this inside IF condition newstring = newstring.replace(letter,"")

(also initialize newstring = string before for loop)

Upvotes: 0

nik_97
nik_97

Reputation: 353

That's because you're setting newstring to a different string output on every iteration of the loop newstring = string.replace("")

You need to set newstring to the replaced string, and then run the next iteration of replace on newstring. Like this:

def remove_vowels(string):
    vowels = ['a','e','i','o','u']
    newstring = string

    for letter in newstring:
        if letter in vowels:
            newstring = newstring.replace(letter , "")

    return newstring

string = "stack overflow"
print("Original string = ", string)
print("String with vowels removed = ", remove_vowels(string))

Output:

Original string = stack overflow
String with vowels removed = stck vrflw

Upvotes: 6

Sumukh Balu
Sumukh Balu

Reputation: 1

Make the following modifications to your code and it should work like a charm :)

def remove_vowels(string):
vowels = 'aeiou'

for letter in string:
    if letter in vowels:
        string = string.replace(letter," ")
    else:
         pass

return string

Upvotes: 0

Related Questions