Python delete all numbers

Remove numbers from string in Python

In this article, we will learn to delete numerical values from a given string in Python. We will use some built-in functions and some custom codes as well. Let’s first have a quick look over what is a string in Python.

Python String

The string is a type in python language just like integer, float, boolean, etc. Data surrounded by single quotes or double quotes are said to be a string. A string is also known as a sequence of characters.

string1 = "apple" string2 = "Preeti125" string3 = "12345" string4 = "pre@12"

A string in Python can contain numbers, characters, special characters, spaces, commas, etc. We will learn four different ways to remove numbers from a string and will print a new modified string.

Example: Remove Numbers from String using regex

Python provides a regex module that has a built-in function sub() to remove numbers from the string. This method replaces all the occurrences of the given pattern in the string with a replacement string. If the pattern is not found in the string, then it returns the same string.

Читайте также:  table-layout

In the below example, we take a pattern as r’4’ and an empty string as a replacement string. This pattern matches with all the numbers in the given string and the sub() function replaces all the matched digits with an empty string. It then deletes all the matched numbers.

#regex module import re #original string string1 = "Hello!James12,India2020" pattern = r'7' # Match all digits in the string and replace them with an empty string new_string = re.sub(pattern, '', string1) print(new_string)

Example: Remove Numbers from String using join() & isdigit()

This method uses isdigit() to check whether the element is a digit or not. It returns True if the element is a digit. This method uses for loop to iterate over each character in the string.

The below example skips all numbers from the string while iterating and joins all remaining characters to print a new string.

string1 = "Hello!James12,India2020" #iterating over each element new_string = ''.join((x for x in string1 if not x.isdigit())) print(new_string)

Example: Remove Numbers from String using translate()

This method uses string Python library. With the help of a string object, maketrans() separates numbers from the given string. Afterward, a translation table is created where each digit character i.e. ‘0’ to ‘9’ will be mapped to None and this translation table is passed to translate() function.

The below example creates a translation table and replaces characters in string based on this table, so it will delete all numbers from the string

import string string1 = "Hello!James12,India2020" #digits are mapped to None translation_table = str.maketrans('', '', string.digits) #deletes all number new_string = string1.translate(translation_table) print(new_string)

Example: Remove Numbers from String

This example uses the filter() and lambda in the generating expression. It filters or deletes all the numbers from the given string and joins the remaining characters of the string to create a new string.

The filter() function uses the original string and lambda expression as its arguments. First, we filtered all digit characters from a string and then joined all the remaining characters.

#original string string1 = "Hello!James12,India2020" #Filters all digits new_string = ''.join(filter(lambda x: not x.isdigit(), string1)) print(new_string)

Conclusion

In this article, we learned to remove numerical values from the given string of characters. We used different built-in functions such as join() , isdigit() , filter() , lambda , sub() of regex module. We used custom codes as well to understand the topic.

Источник

Python: Remove all numbers from string

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,

Frequently Asked:

import re org_string = "Sample 11 String 42 -In 2020" pattern = r'4' # Match all digits in the string and replace them by empty string mod_string = re.sub(pattern, '', org_string) print(mod_string)

Here we passed a pattern r’4’ & 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)

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)

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,

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)

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'8' # 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()
****** 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

Источник

Python: Remove all Numbers from String

Remove all Numbers from String

A Python string is a collection of characters surrounded by single, double, or triple quotes. The computer does not understand the characters; instead, it stores the manipulated character as a combination of 0’s and 1’s internally.

string = "This is 2021 BTech21Geeks"

Delete all Numbers from the string

There are several ways to remove all numbers from the string some of them are:

Method #1:Using string.join() method

The string.join(iterable) method takes an iterable object as input, joins its elements together using the string’s value as a separator, and returns the resulting string as output.

To remove numbers from the string, we will first iterate through it and select non-digit values, which we will then pass to the string.

To join them, use the join() method and output the resulting string with non-digit characters.

Below is the implementation:

# given string string = "This is 2021 BTech21Geeks" # using string.join() method by traversing the string and element is not digit string = ''.join((element for element in string if not element.isdigit())) # print the string print(string)

Method #2:Using translate() function

This method makes use of the string Python library. maketrans() uses a string object to separate numbers from the given string. Following that, a translation table is created in which each digit character, i.e. ‘0’ to ‘9,’ is mapped to None, and this translation table is passed to the translate() function.

The example below creates a translation table and replaces characters in a string based on this table, removing all numbers from the string.

Below is the implementation:

import string # given string string = "This is 2021 BTech21Geeks" # the digits in string are mapped to empty character table = str.maketrans('', '', string.digits) # remove all numbers from string string = string.translate(translation_table) # print the string print(string)

Method #3:Using Regex

Python includes a regex module with a built-in function sub() for removing numbers from strings. This method uses a replacement string to replace all occurrences of the given pattern in the string. If the pattern is not found in the string, the same string is returned.

In the following example, we use r’6’ as a pattern and an empty string as a replacement string. This pattern matches all of the numbers in the given string, and the sub() function replaces all of the digits that match with an empty string. It then deletes all of the numbers that match.

Below is the implementation:

import re # given string string = "This is 2021 BTech21Geeks" # digits pattern digitspattern = r'2' # Replace all of the digits in the string with an empty string. string = re.sub(digitspattern, '', string) # print the string print(string)

Method #4:Using filter() and join()

In the generating expression, this example employs the filter() and lambda functions. It filters or deletes all of the numbers from the given string and then joins the remaining characters to form a new string.

The original string and the lambda expression are passed to the filter() function as arguments. We started by removing all digit characters from a string, then we joined all of the remaining characters.

Below is the implementation:

# given string string = "This is 2021 BTech21Geeks" # removing all digits string = ''.join(filter(lambda x: not x.isdigit(), string)) # print the string print(string)

Related Programs:

Источник

Оцените статью