How to Remove Numbers from Text Using Python


Here we remove the zeros and threes from the string variable text.

To do this we first create a mapping using maketrans that declares any values in string.digits (which is the the digits from 0 to 9) should be removed.

We then apply the mapping to our text variable using the translate function.

 1|  import string
 3|  text = '000This text33 has some numbers'
 5|  mapping = str.maketrans('', '', string.digits)
 6|  text = text.translate(mapping)
 8|  >> 'This text has some numbers'
Did you find this snippet useful?

Sign up for free to to add this to your code library