How do i remove all text from a number in python?

How can I remove digits from a string?

How do i remove all text from a number in python?

congusbongus

12.4k5 gold badges69 silver badges94 bronze badges

asked Oct 12, 2012 at 3:27

8

Would this work for your situation?

>>> s = '12abcd405'
>>> result = ''.join([i for i in s if not i.isdigit()])
>>> result
'abcd'

This makes use of a list comprehension, and what is happening here is similar to this structure:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
    if not i.isdigit():
        no_digits.append(i)

# Now join all elements of the list with '', 
# which puts all of the characters together.
result = ''.join(no_digits)

As @AshwiniChaudhary and @KirkStrauser point out, you actually do not need to use the brackets in the one-liner, making the piece inside the parentheses a generator expression (more efficient than a list comprehension). Even if this doesn't fit the requirements for your assignment, it is something you should read about eventually :) :

>>> s = '12abcd405'
>>> result = ''.join(i for i in s if not i.isdigit())
>>> result
'abcd'

answered Oct 12, 2012 at 3:34

RocketDonkeyRocketDonkey

35.2k7 gold badges77 silver badges84 bronze badges

7

And, just to throw it in the mix, is the oft-forgotten str.translate which will work a lot faster than looping/regular expressions:

For Python 2:

from string import digits

s = 'abc123def456ghi789zero0'
res = s.translate(None, digits)
# 'abcdefghizero'

For Python 3:

from string import digits

s = 'abc123def456ghi789zero0'
remove_digits = str.maketrans('', '', digits)
res = s.translate(remove_digits)
# 'abcdefghizero'

answered Oct 12, 2012 at 9:46

How do i remove all text from a number in python?

Jon ClementsJon Clements

134k31 gold badges240 silver badges273 bronze badges

3

Not sure if your teacher allows you to use filters but...

filter(lambda x: x.isalpha(), "a1a2a3s3d4f5fg6h")

returns-

'aaasdffgh'

Much more efficient than looping...

Example:

for i in range(10):
  a.replace(str(i),'')

How do i remove all text from a number in python?

fredtantini

14.9k8 gold badges46 silver badges54 bronze badges

answered Oct 12, 2012 at 4:19

Alain NisamAlain Nisam

6623 silver badges13 bronze badges

2

What about this:

out_string = filter(lambda c: not c.isdigit(), in_string)

answered Oct 12, 2012 at 3:44

Pavel PaulauPavel Paulau

7661 gold badge7 silver badges19 bronze badges

2

Just a few (others have suggested some of these)

Method 1:

''.join(i for i in myStr if not i.isdigit())

Method 2:

def removeDigits(s):
    answer = []
    for char in s:
        if not char.isdigit():
            answer.append(char)
    return ''.join(answer)

Method 3:

''.join(filter(lambda x: not x.isdigit(), mystr))

Method 4:

nums = set(map(int, range(10)))
''.join(i for i in mystr if i not in nums)

Method 5:

''.join(i for i in mystr if ord(i) not in range(48, 58))

3

Say st is your unformatted string, then run

st_nodigits=''.join(i for i in st if i.isalpha())

as mentioned above. But my guess that you need something very simple so say s is your string and st_res is a string without digits, then here is your code

l = ['0','1','2','3','4','5','6','7','8','9']
st_res=""
for ch in s:
 if ch not in l:
  st_res+=ch

answered Oct 12, 2012 at 4:09

iddqdiddqd

1,2791 gold badge8 silver badges9 bronze badges

I'd love to use regex to accomplish this, but since you can only use lists, loops, functions, etc..

here's what I came up with:

stringWithNumbers="I have 10 bananas for my 5 monkeys!"
stringWithoutNumbers=''.join(c if c not in map(str,range(0,10)) else "" for c in stringWithNumbers)
print(stringWithoutNumbers) #I have  bananas for my  monkeys!

answered Oct 12, 2012 at 3:34

How do i remove all text from a number in python?

Sean JohnsonSean Johnson

5,5212 gold badges15 silver badges22 bronze badges

If i understand your question right, one way to do is break down the string in chars and then check each char in that string using a loop whether it's a string or a number and then if string save it in a variable and then once the loop is finished, display that to the user

answered Oct 12, 2012 at 3:34

BaahubaliBaahubali

4,9526 gold badges31 silver badges70 bronze badges

1

How do I remove numbers from text in Python?

In Python, an inbuilt function sub() is present in the regex module to delete numbers from the Python string. The sub() method replaces all the existences of the given order in the string using a replacement string.

How do you strip a number in Python?

Remove Numbers From String in Python.
Remove Numbers From the String Using string.join() Method in Python..
Remove Numbers From the String in Python Using the string.translate() Method..
Remove Numbers From the String in Python Using the re.sub() Method..

How do I remove a string from a number?

Get the String to remove all the digit..
Convert the given string into a character array..
Initialize an empty string that stores the result..
Traverse the character array from start to end..
Check if the specified character is not digit then add this character into the result variable..
Now, print the result..

How do you remove text in Python?

Using translate(): translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .