Strip number from string python

Here's a method using str.join(), str.isnumeric(), and a generator expression which will work in 3.x:

>>> my_str = '123Hello, World!4567'
>>> output = ''.join(c for c in my_str if not c.isnumeric())
>>> print(output)
Hello, World!
>>> 

This will also work in 2.x, if you use a unicode string:

>>> my_str = u'123Hello, World!4567'
>>> output = ''.join(c for c in my_str if not c.isnumeric())
>>> print(output)
Hello, World!
>>> 

Hmm. Throw in a paperclip and we'd have an episode of MacGyver.

Update

I know that this has been closed out as a duplicate, but here's a method that works for both Python 2 and Python 3:

>>> my_str = '123Hello, World!4567'
>>> output = ''.join(map(lambda c: '' if c in '0123456789' else c, my_str))
>>> print(output)
Hello, World!
>>>

  1. HowTo
  2. Python How-To's
  3. Remove Numbers From String in Python

Created: March-06, 2021 | Updated: October-17, 2021

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

This tutorial will look into various methods to remove the numbers or digits from the string in Python. We usually remove numbers from the data in the Natural Language Processing during the process of Data Cleaning.

Suppose we have a string abcd1234efg567, and we want to remove the digits from the string to get a string like abcdefg. We can remove the numbers from the string in Python using the following methods:

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

The string.join(iterable) method takes an iterable object iterable as input, joins its elements together using the value of the string as a separator, and returns the resulting string as an output.

To remove numbers from the string, we will first iterate through the string and select non-digit values, pass them to the string.join() method to join them and get the resulting string with non-digit characters as output.

The below example code demonstrates how to use the string.join() method to remove the numbers from the string in Python.

string = 'abcd1234efg567'
newstring = ''.join([i for i in string if not i.isdigit()])
print(newstring)

Output:

abcdefg

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

The string.translate(map) method in Python 2, takes a mapping table or dictionary as input and returns the string after replacing the specified characters with the characters defined in the input mapping table or dictionary.

The below example code demonstrates how to use the string.translate() method to remove the numbers from the string in Python 2.

from string import digits

string = 'abcd1234efg567'
newstring = string.translate(None, digits)
print(newstring)

Output:

abcdefg

In Python 3, the string.translate(table) takes the translation table as input instead of the mapping table or dictionary, as in Python 2. Therefore, we need to use the str.maketrans() method to get a translation table to use it as an input for the string.translate() method.

The below example code demonstrates how to use the string.translate() and str.maketrans() methods to remove the numbers from the string in Python 3:

from string import digits

string = 'abcd1234efg567'
table = str.maketrans('', '', digits)
newstring = string.translate(table)
print(newstring)

Output:

abcdefg

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

The re.sub(pattern, replace, string) takes the string as input and returns the string by replacing the non-overlapping occurrences of the pattern string (described as a regular expression) with the replace value in the string.

The regular expression for digits is [0-9]+. We just need to pass this as the pattern argument and '' as replace to remove the numbers from the input string using the re.sub() method.

The below example code demonstrates how to use the re.sub() method to remove numbers from the string:

import re
string = 'abcd1234efg567'
newstring = re.sub(r'[0-9]+', '', string)
print(newstring)

Output:

abcdefg

Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • Strip number from string python

    How do you strip a number from a string 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 all numbers from a string?

    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 I remove numbers from a string in a DataFrame Python?

    “How to remove numbers in pandas dataframe column” Code Answer.
    import pandas as pd..
    # Example DataFrame..
    df = pd. DataFrame. from_dict({'Name' : ['May21', 'James', 'Adi22', 'Hello', 'Girl90'],.
    'Volume': [23, 12, 11, 34, 56],.
    'Value' : [21321, 12311, 4435, 32454, 654654]}).
    df['Name'] = df['Name']. str..

    How do I remove digits from a number in Python?

    Note that Python integers are immutable, so the only way to remove a digit from an integer is to create a new integer that doesn't contain the digit. An alternative approach is to use string slicing. To remove the last digit from an integer: Use the str() class to convert the integer to a string.