Python: Remove all Numbers from String

A Python string is a collection of characters surrounded by single, double, or triple quotes. The computer does not understand the characters; instead, it stores the manipulated character as a combination of 0’s and 1’s internally.

Examples:

Input:

string = "This is 2021 BTech21Geeks"

Output:

This is  BTechGeeks

Delete all Numbers from the string

There are several ways to remove all numbers from the string some of them are:

Method #1:Using string.join() method

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

To remove numbers from the string, we will first iterate through it and select non-digit values, which we will then pass to the string.

To join them, use the join() method and output the resulting string with non-digit characters.

Below is the implementation:

# given string
string = "This is 2021 BTech21Geeks"
# using string.join() method by traversing the string and element is not digit
string = ''.join((element for element in string if not element.isdigit()))
# print the string
print(string)

Output:

This is  BTechGeeks

Method #2:Using translate() function

This method makes use of the string Python library. maketrans() uses a string object to separate numbers from the given string. Following that, a translation table is created in which each digit character, i.e. ‘0’ to ‘9,’ is mapped to None, and this translation table is passed to the translate() function.

The example below creates a translation table and replaces characters in a string based on this table, removing all numbers from the string.

Below is the implementation:

import string
# given string
string = "This is 2021 BTech21Geeks"

# the digits in string  are mapped to empty character
table = str.maketrans('', '', string.digits)

# remove all numbers from string
string = string.translate(translation_table)
# print the string
print(string)

Output:

This is  BTechGeeks

Method #3:Using Regex

Python includes a regex module with a built-in function sub() for removing numbers from strings. This method uses a replacement string to replace all occurrences of the given pattern in the string. If the pattern is not found in the string, the same string is returned.

In the following example, we use r'[0-9]’ as a pattern and an empty string as a replacement string. This pattern matches all of the numbers in the given string, and the sub() function replaces all of the digits that match with an empty string. It then deletes all of the numbers that match.

Below is the implementation:

import re
# given string
string = "This is 2021 BTech21Geeks"
# digits pattern
digitspattern = r'[0-9]'

# Replace all of the digits in the string with an empty string.
string = re.sub(digitspattern, '', string)

# print the string
print(string)

Output:

This is  BTechGeeks

Method #4:Using filter() and join()

In the generating expression, this example employs the filter() and lambda functions. It filters or deletes all of the numbers from the given string and then joins the remaining characters to form a new string.

The original string and the lambda expression are passed to the filter() function as arguments. We started by removing all digit characters from a string, then we joined all of the remaining characters.

Below is the implementation:

# given string
string = "This is 2021 BTech21Geeks"
# removing all digits
string = ''.join(filter(lambda x: not x.isdigit(), string))

# print the string
print(string)

Output:

This is  BTechGeeks

Leave a Comment