3

For my assignment, I have to create a function that returns a new string that is the same as the given string, but with digits removed.

Example: remove digits(’abc123’) would return the string ’abc’.

I have tried almost everything I can think off but it's not working properly :(

def test(str):
    for ch in str:
        num = ['0', '1', '2', '3', '4', '6', '7', '8', '9']
        if ch == num[0]:
            return str.replace(ch, '')
        elif ch == num[1]:
            return str.replace(ch, '')
        elif ch == num[2]:
            return str.replace(ch, '')
        elif ch == num[3]:
            return str.replace(ch, '')
        elif ch == num[4]:
            return str.replace(ch, '')
        elif ch == num[5]:
            return str.replace(ch, '')
        elif ch == num[6]:
            return str.replace(ch, '')
        elif ch == num[7]:
            return str.replace(ch, '')
        elif ch == num[8]:
            return str.replace(ch, '')

I enter test('abc123'), expecting the output to be 'abc'. But instead I get 'abc23' as my output.

In other attempts, same problem:

def test(str):
    for char in str:
        num = ['0', '1', '2', '3', '4', '6', '7', '8', '9']
        if char in list(num):
            return str.replace(char, '', len(str))

I get the same results.

Can anyone help me? It would be greatly appreciated.

2
  • It's because you are returning from your funciton when you first encounter a number. return stops execution and exits the function! Commented Oct 6, 2016 at 22:25
  • Also, you can just check if char in "0123456789" which will simplify your code. Commented Oct 6, 2016 at 22:26

5 Answers 5

9

Use regex

import re
def test(str):
    string_no_numbers = re.sub("\d+", " ", str)
    print(string_no_numbers)
test('abc123') #prints abc
Sign up to request clarification or add additional context in comments.

Comments

2

Use this

string = ''.join([c for c in string if c not in "1234567890"])

Comments

1

Your second solution is close:

def drop_digits(in_str):
    digit_list = "1234567890"
    for char in digit_list:
        in_str = in_str.replace(char, "")

    return in_str

The original problem is that you return after replacing only the '1' chars.

You can also do this with a list comprehension:

return ''.join([char for char in in_str if not char.isdigit()])

3 Comments

Might want to use a better variable name than the OP ;)
Good point. Changing ...
thanks dude, really apreciate it
0

You need to read up on some python basics. The "return" exits your function, so it will only ever modify one entry.

Here's a hint: replace str with an updated copy instead of returning it.

str = str.replace(ch, '')

4 Comments

As for python basics... we are dealing with a function not a method here. This ain't Java ;)
eh, test could be in a class without the "self" argument here, if we are gonna be pedantic ;)
If we want to be super pedantic, test is not either because it has a indentation errors.
^ definitely gotta agree
-1
def test(string):
    return ''.join((letter for letter in string if not letter.isdigit()))

3 Comments

This removes more than just digits.
probably want to check if not letter.isdigit()
I've used not letter.isdigit() but thought it will be kind of overkill and changed it to .isalpha, but re-changed it now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.