0

I am very new to python and going through some course material and wrote this function to remove a specific character from string no matter how many times that character is in the string.

def remove_letter(): #Remove a selected letter from a string
    base_string = str(raw_input("Enter String: "))
    letter_remove = str(raw_input("Enter Letter: ")) #takes any size string
    letter_remove = letter_remove[0]
    string_length = len(base_string)
    location = 0

    while (location < string_length): #by reference (rather than by value)
        if base_string[location] == letter_remove:
            base_string = base_string[:location] + base_string[location+1::]
            string_length -= 1
        location+=1

    print "Result: %s" % base_string
    return

Now here is what i am not understanding, if i put "asdfasdfasdf" in the string and then choose to remove the letter "d" it works perfect. But if put "Hello" in the string and choose to remove the letter "l" it will only remove one "l" and the result will be "Helo". I can't understand why its working when i put "asdfasdfasdf" and now "Hello"

Nick
  • 7,103
  • 2
  • 21
  • 43
  • Why not use a built-in function for this instead of creating your own? `'ababa'.replace('a','') => 'bb'` – Nick Oct 28 '16 at 20:21
  • I'm learning python now, im sure there are a bunch of different ways to do this and a lot easier but i like to learn by trying out different things to see how everything works, and i really wanted to find out why this isn't working – Mafioso1823 Oct 28 '16 at 20:23
  • If you are just now learning Python, I would personally recommend that you learn with Python 3 instead of 2. – Nick Oct 28 '16 at 20:26
  • The real issue here is that hes removing items as he is iterating through the list, that's a big no no in python. If you have to do it for what ever reason, iterate from the back of the list to the front. – MooingRawr Oct 28 '16 at 20:32

4 Answers4

0

The problem is that you've got location+=1 outside your if-statement.

Your code as-is skips the letter after a removed letter.

Since you're doing both string_length -= 1 AND location+=1 in an iteration when you remove a letter, location is effectively moving two indices.

To fix it though, you'll need to do a little more than just that, since location+=1 is ALSO required outside of that if-statement.

I just explained what was going wrong, and I've got to run right now, but I see some other people have already given you solutions, so I'm not worried. Good luck!

B. Eckles
  • 1,626
  • 2
  • 15
  • 27
0

Consider

#base_string is 'Hello', location is 2 and string_length is 5.
base_string = base_string[:location] + base_string[location+1::] #'Helo'

Then you reduce the string length and increment the location. You have location is 3 , but 'Helo'[3] == 'o' not 'l'. When you remove an element from your string, you are essentially shifting all the remaining characters by 1, so you should not update location, as it will already be now pointing to the next character.

while (location < string_length): #by reference (rather than by value)
    if base_string[location] == letter_remove:
        base_string = base_string[:location] + base_string[location+1::]
        string_length -= 1
    else:
       location+=1
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
0

It is a bug. It doesn't work properly only when the same character appear consecutively.

  1. Hello, it ignores the second l in Hello when it met the first l
  2. If you try Helol, it will remove both ls.
  3. If you try Helllo, the result would be Helo.

Solution: When you encounter the target letter, remove it and continue iterating the rest characters of your updated string other than increase location

Add continue will fix your issue.

while (location < string_length): #by reference (rather than by value)
    if base_string[location] == letter_remove:
        base_string = base_string[:location] + base_string[location+1::]
        string_length -= 1
        continue
    location+=1

Test:

python2 test.py                                                                                   Fri 28 Oct 14:25:59 2016
Enter String: Hello
Enter Letter: l
Result: Heo
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
-1

You could always use string replacement.

base_string.replace(letter_remove,"")
Simon Black
  • 913
  • 8
  • 20