In this article we will discuss different ways to remove characters except digits from string in Python.

Remove all numbers from string using regex

Python’s regex module provides a function sub() i.e.

re.sub(pattern, repl, string, count=0, flags=0)

It returns a new string. This new string is obtained by replacing all the occurrences of the given pattern in the string by a replacement string repl. If the pattern is not found in the string, then it returns the same string.

Let’s use this to delete all numbers or digits from string in python,

import re

org_string = "Sample 11 String 42 -In 2020"

pattern = r'[0-9]'
# Match all digits in the string and replace them by empty string
mod_string = re.sub(pattern, '', org_string)

print(mod_string)

Output:

Sample  String  -In

Here we passed a pattern r'[0-9]’ & an empty string as replacement string to the sub() function. This pattern matched all the digits in the original string and sub() function replaced all the matched digits by an empty string.

So, this is how we can delete all numbers / digits from a string using regex in python.

Remove all numbers from string using join() & Generator expression

Iterate over all the characters in the string using for loop in a generator expression. Skip all digit characters and join remaining characters to create a new string i.e.

org_string = "Sample 11 String 42 -In 2020"

# Iterate over the characters in string and join all characters except
# digits, to create a new string.
mod_string = ''.join((item for item in org_string if not item.isdigit()))

print(mod_string)

Output:

Sample  String  -In

It deleted all the numbers / digits from the string.

Remove all numbers from string using translate()

Create a translation table where each digit character i.e. ‘0’ to ‘9’ will be mapped to None and pass this translation table to translate() function i.e.

import string

org_string = "Sample 11 String 42 -In 2020"

# Create a translation table, where all digits are mapped to None
translation_table = str.maketrans('', '', string.digits)

# replace characters in string based on translation table, so it will
# delete all numbers / digits from the string
mod_string = org_string.translate(translation_table)

print(mod_string)

Output:

Sample  String  -In

It deleted all the numbers / digits from the string.

How did it worked?

maketrans() function creates a translation table used by translate() function. If we pass first 2 arguments as empty and a string in the third argument, then all the characters in the third argument will be mapped to None. So, we created a translation table using maketrans() function, where all digits are mapped to None i.e.

# Create a translation table, where all digits are mapped to None
translation_table = str.maketrans('', '', string.digits)

Contents of translation_table are,

{48: None, 
49: None, 
50: None, 
51: None, 
52: None, 
53: None, 
54: None, 
55: None, 
56: None, 
57: None}

Now we passed this translation table to translate() function, which replaced all the digits in the string to None based on our translation table. So, this is how we can delete all numbers / digits in a string in python using translate() function.

Remove all numbers from string using filter() & join()

Filter all digit characters from a string and join the remaining characters to create a new string i.e.

org_string = "Sample 11 String 42 -In 2020"

# Filter all digits from characters in string & join remaining characters
mod_string = ''.join(filter(lambda item: not item.isdigit(), org_string))

print(mod_string)

Output:

Sample  String  -In

It deleted all the numbers / digits from the string.

How did it worked?

We passed a lambda function and original string to the filter() function. Filter() function iterated over all characters in string and called the given lambda function on each character. It returned the characters for which our lambda function returned False. Then we joined back all those filtered characters to create a new string. So, basically first we filtered all digit characters from a string and then joined all the remaining characters.

These were 3 different ways to delete all digits or numbers from a string in python.

The complete example is as follows,

import re
import string

def main():

    print('****** python remove all numbers from string ******')

    print('*** Remove all numbers from string using regex ***')

    org_string = "Sample 11 String 42 -In 2020"

    pattern = r'[0-9]'
    # Match all digits in the string and replace them by empty string
    mod_string = re.sub(pattern, '', org_string)

    print(mod_string)

    print('*** Remove all numbers from string using join() & Generator expression ***')

    org_string = "Sample 11 String 42 -In 2020"

    # Iterate over the characters in string and join all characters except
    # digits, to create a new string.
    mod_string = ''.join((item for item in org_string if not item.isdigit()))

    print(mod_string)

    print('*** Remove all numbers from string using translate() ***')

    org_string = "Sample 11 String 42 -In 2020"

    # Create a translation table, where all digits are mapped to None
    translation_table = str.maketrans('', '', string.digits)

    # replace characters in string based on translation table, so it will
    # delete all numbers / digits from the string
    mod_string = org_string.translate(translation_table)

    print(mod_string)

    print('*** Remove all numbers from string using filter() & join() ***')

    org_string = "Sample 11 String 42 -In 2020"

    # Filter all digits from characters in string & join remaining characters
    mod_string = ''.join(filter(lambda item: not item.isdigit(), org_string))

    print(mod_string)

if __name__ == '__main__':
    main()

Output:

****** python remove all numbers from string ******
*** Remove all numbers from string using regex ***
Sample  String  -In 
*** Remove all numbers from string using join() & Generator expression ***
Sample  String  -In 
*** Remove all numbers from string using translate() ***
Sample  String  -In 
*** Remove all numbers from string using filter() & join() ***
Sample  String  -In

Join a list of 2000+ Programmers for latest Tips & Tutorials