Python

Find and Remove Numbers in String in Python

Strings, like integers, floats, and Booleans, are data types in Python. A string is a data that is enclosed by single or double quotations. It is additionally referred to as a “character sequence.”

In this article, we will look at several methods for removing numeric data from a Python string. During Data Cleaning, we normally eliminate data. Assume we have a sample string “abc123klm456” and want to remove the numbers to obtain a python string that just includes the character, “able.” Python strings include characters, numerals, delimiters, spaces, and so on.

Example 01:

In the following example, we used a for loop to compare the given string to a string of digits and then generated a new string by discarding only the digits. In the first line, we have our original string variable named ‘a’ which comprises characters and numbers merged. We created a new string variable named ‘numbers’ which includes all of the digits from 0 to 9. In the third line, another empty string type variable is created and named ‘b.’ A for loop is created with an ‘if’ condition that compares every element of the variable ‘a’ (original string) with the string of numbers. If the element in the original string is not found in the string ‘numbers,’ the element of ‘a’ is added to the new string ‘b.’ After the for loop is completed, the variable ‘b’ will comprise only characters of our original string, thereby eliminating all the digits.

Finally, by using the print command, the variable ‘b’ is demonstrated there at the output terminal, according to the snapshot below.

Example 02:

In the following example, we used an inbuilt Python function, join(), and alpha(). Python’s join() method is used to connect a string with iterable components. It allows you to use various iterables such as tuples, lists, strings, and so on. The function alpha() specifies whether the attribute is an alphabet or perhaps a digit. If the function detects a character, it returns True.

In the above-mentioned code, the original string, which we have to change, is stored in the variable name ‘a.’ The next line employs Python’s join() function with a for loop as its parameter, as well as an if the condition is alpha(). The for loop iterates all over the original string, and the if condition only passes the element. If it is a character and discards anything other than character, the new string is stored in the variable named ‘b’ which only comprises characters. Lastly, using the print command, the variable ‘b’ is presented on the output side, as seen in the picture below.

Example 03:

digit() is another Python inbuilt function that can be used as a numeric digit checker. The is digit() function specifies if the element is an alphabet or a digit. If the function detects a number, it returns True. Hence, it works as an inverse of the alpha() function. This method is very similar to the preceding one.

The original string is stored in the variable named ‘a.’ In the next line, the join() function is used with a for loop, which iterates across the original string we have, and an “if not” condition with the digit() function. Since the “if not” condition only returns True if there is not a numeric digit found, the join() function will join all the elements present in ‘a’ while discarding all the numeric digits and storing them in the variable named ‘b.’ Finally, the converted string ‘b’ is displayed at the output terminal using the print command, as shown in the screenshot below.

Example 04:

Another way to remove numbers from the string is by using the translate() function. First, we need to import the string Python library to use the make trans() function. The make trans() function separates digits from a string stream when using a string object. Then, it generates a table in which every integer from ‘zero’ to ‘nine’ is plotted to None. We can then send this translation table as a parameter to the translate() function which eliminates all the digits in the string and generates a new string without any numbers.

In the above code, we first imported the string Python library. Then, we stored our original string with numbers that we need to change in a variable named ‘a.’ In the next line, we used make a trans() function with string digits in its parameter to create a table of digits from 0 to 9. Then, this translation table is passed to the translate function() of object ‘a.’ It returns a new string replacing the specified numbers. In this case, 0 to 9, and stores it in the new string variable named ‘b’. The new string ‘b’ is then displayed at the output terminal using the print command as shown in the screenshot below.

Example 05:

Another remarkably analogous way to the previous example is to import digits as well along with strings from the python library. This would allow us to directly use the ‘digits’ constant instead of calling ‘string digits.’

We stored our original string with numbers that we need to change in a variable named ‘a.’ In the next line, we used to make a trans() function with just the ‘digits’ constant in its parameter to store a table of digits from 0 to 9. Then, this translation table is passed to the translate function() of object ‘a.’ It returns a new string replacing the specified numbers. In this case, 0 to 9, and stores it in the new string variable named ‘c’. As seen in the snippet below, the new string ‘c’ is then showcased there at the output terminal using the print function.

Example 06:

In this example, we will be using ‘ord’ or ‘ordinal’ to compare the elements of our original string with their Unicode code to remove numbers. We know the numbers or digits in python have Unicode values from 48 to 57 so we can just eliminate the ord values of this range and keep the rest characters.

In the above code, the original text with numbers is stored in a variable named ‘a.’ In the next line, a new empty string variable ‘b’ is initialized. Then, a for loop is traversed through the string a, element wise. The characters with Unicode values in the range of 10 to 100 are discarded and the rest of the characters are added to the new string ‘b’. It is then displayed at the output terminal using the print command as shown in the screenshot below.

Conclusion:

In this article, we demonstrated how to remove integer numbers from a string of characters. To learn how to remove digits from a Python string, we utilized built-in techniques and custom code such as join(), is a digit(), and translation(). We used all these functions in several examples and illustrated how these functions work with different variables and also used several functions together to achieve the task collectively. We were able to identify and remove numbers from a string simultaneously in python programming language as it is poses very lucrative and efficient functions to assist in challenging situations.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.