Python

Remove Number from String Python

In this tutorial, we will explore various approaches to erasing the numbers from the Python string. We usually remove data during Data Cleaning. Like we have a sample string “efg123abc567”, and we wish to eliminate the numbers from the string to acquire a python string that only contains characters. Python string contains characters, numbers, delimiters, spaces, etc. To separate numbers from the string, we use various built-in commands and implement them with the help of the Spyder Compiler in Windows 10.

Example 1:

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. If you can’t find the order in the string, then the resulting string is the same.

To understand the sub() function concept, all you need to do is open the program file and implement the code. So, in our first program code, we first import a python regex module and then create a string containing a number and characters. We can then define a pattern that removes numbers from 0 to 9. After this, we call a sub() command, which takes three arguments as a parameter, i.e., string, blankstring, pattern. The function first iterates over the string, removes the number ranging from 0 to 9, and swaps them with a blank string. Then it prints the new filtered string. The program code that is explained is written here.

import re

first_str = "Welcome! Python12, World2021"

order = r'[0 - 9]'

filtered_string = re.sub(order, '', first_str)

print(filtered_string)

Save and execute the program code to view how the sub() function removes the numbers from the string. The console screen displays the result.

Example 2:

There is an inbuilt function join() and isdidgit() to delete numbers from the Python string in Python. The join() function in python is utilized to join a string with iterable elements. It lets you use various iterables such as tuple, list, string, etc. The isdigit() method checks whether the value is a number or a string. If the function finds a digit, then it returns True.

We use a for loop to traverse over each element in the string. So, in our second program code, we first create a string that contains a number and characters. Then we use the join() and isdigit() method with a for a loop. It traverses over the string while ignoring all the numbers present in the string and joins the remaining characters to form a new filtered string. The print function displays the filtered string on the console screen. The program code that is explained is written here.

My_string = "Python12, Progrmming2021"

new_string = ''.join((x for x in my_string if not x.isdigit()))

print(new_string)

Save and run the program file to view how join() and isdigit() function remove the digits from the string. The console screen displays the result.

Example 3:

In this example code, we use a built-in string python library. While using a string object, maketrans() splits digits from the stream of string. Then it creates a table where every number, i.e., ‘zero’ to ‘nine’ will be plotted to None. We can then pass this translation table to the translate() method as a parameter. So, here we first import a string library and then create a string that contains a number and characters. Then we create a dictionary or a table where numbers are mapped to None. Afterward, we call the translate function and pass the table as an argument. As it removes all the digits and generates a new filtered string. The program code that is explained is written here.

import string

my_string = "Python120, Code2021"

dict_table = str.maketrans('', '', string.digits)

new_string = my_string.translate(dict_table)

print(new_string)

To check the output, save and run the program just like we did in all program codes. The console screen displays the result.

Conclusion

In this tutorial, we explained how to delete integer numbers from the specified string of characters. We used various built-in methods and custom codes like sub(), join(), isdigit(), and translation() to understand how to remove digits from the specified python string.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content