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.
In the below example, we take a pattern as r'[0-9]’ 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'[0-9]'
# Match all digits in the string and replace them with an empty string
new_string = re.sub(pattern, '', string1)
print(new_string)
Hello!James,India
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)
Hello!James,India
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)
Hello!James,India
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)
Hello!James,India
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.