| by Arround The Web | No comments

Remove Number From String Python

In Python, string includes numeric values, characters, spaces, delimiters, and many more. More specifically, while data cleaning, users want to separate multi values from the strings or remove them from the string. Multiple built-in methods or operators of Python can be used to perform this particular operation.

This write-up explained different ways to remove the numeric value from the string in Python.

How to Remove Numbers From String in Python?

To eliminate the numbers from any string in Python, the below-listed methods are utilized:

Method 1: Remove Numbers From String in Python Using “join()” and “isdigit()” Methods

Use the “join()” and “isdigit()” built-in methods to remove the numeric value from any string in Python. The “join()” method is used to join a string with the specified iterable elements, such as a list, tuple, string, and many more. On the other hand, the “isdigit()” method checks whether the element is a string or a number.

Example

At first, create a string variable “alpha_num_str” and initialize it:

alpha_num_str = "Welcome7 to LinuxHint022"

Use the “join()” and “isdigit()” functions with a “for” loop to traverse over every value from the previously created string and ignore any numeric value that exists in the string. Then, join the remaining string characters to generate a new filtered string without numeric values and save it in the new string variable:

char_str = '' .join((z for z in alpha_num_str if not z.isdigit()))

Get the value of filtered variable through the print function:

print(char_str)

It can be observed that the newly generated string has no numeric value:

Method 2: Remove Numbers From String in Python Using “sub()” Built-in Method

Python built-in “sub()” method can be used to delete the numeric values from the string. The “sub()” method exists in the regex module that replaces all the present of the provided pattern in the string with a particular replacement string.

Syntax

Check the syntax of the “sub()” method:

sub(pattern, replaceValue, string)

Here:

  • pattern” represents the string that requires to be replaced.
  • replaceValue” is the value that will be replaced with the original value.
  • string” argument is the original string.

Example

First, import the Python built-in “re” regex module:

import re

Then, define a desired pattern that removes numbers from “0-9” and store it in the “pattern_order” variable:

pattern_order = r'[0-9]'

Call the “re.sub” method with required modules and pass them to the “char_str” string variable:

char_str = re.sub(pattern_order, '', alpha_num_str)

Print the value of the string variable:

print(char_str)

Output

Method 3: Remove Numbers From String in Python Using “translate()” Built-in Method

To remove the numbers from the string in Python, the built-in “string” Python library can be used. When the “string” object is passed, the “maketrans()” method splits digits from the stream of string. After that, it will create a trans-table that keeps the none value against “0” to “9” numbers. Then, this created table will be passed to the “translate()” function as an argument.

Example

Initially, import the “string” Python built-in library:

import string

Use the “str.maketrans()” method to split digits from the stream of string by specifying the required parameters. As a result, the table will be created that includes each digit from “0-9” will be mapped to none and saved into the “trans_table” variable:

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

Call the “translate()” the method with particular string and pass the previously generated table as parameter:

char_str = alpha_num_str.translate(trans_table)

Lastly, use the “print()” statement:

print(char_str)

As you can see, the numbers from the desired string have been eliminated successfully:

Method 4: Remove Numbers From String in Python Using “filter()” Method

To remove the integer value from any Python string, the “filter()” function, and lambda are also used. The “filter()” method filters or removes all the numeric values from the provided string, the lambda expression as its arguments, and joins the rest of the string characters to generate a new modified string through the “join()” method.

Example

First, filter the number from the previously initialized string. Then, call the “join()” method to join all the remaining string characters and passes to the “char_str” string variable:

char_str = ''.join(filter(lambda z: not z.isdigit(), alpha_num_str))

Call the “print()” function to get the desired filtered string:

print(char_str)

Output

That was all about deleting the numeric value from the string in Python.

Conclusion

To delete the number from any Python string, the “join()” and “isdigit()” methods, the “sub()” regex module method, “translate()”, and the “filter()” methods can be used. These methods first filter the string from the numeric value and then save the modified string to the new variable. This write-up explained different ways to remove the numeric value from the string in Python.

Share Button

Source: linuxhint.com

Leave a Reply