You are currently viewing Python String Remove Numbers

How to remove numbers from the string in Python? You can remove numeric digits/numbers from a given string in Python using many ways, for example, by using join() & isdigit(), translate(), re.sub(), filter(), join() & isalpha(), and replace() functions. In this article, I will explain how to remove numbers from a given string by using all these functions with examples.

Advertisements

1. Quick Examples of Removing Numbers From String.

If you are in a hurry, below are some quick examples of how to remove numbers from the string.


# Quick examples of removing numbers

# Initialize the string
string = "welcome2 to34 sparkby56examples01"

# Example 1: Using join() and isdigit() methods
# Remove numbers from string 
modified_string = ''.join([i for i in string if not i.isdigit()])

# Example 2: Using the translate() method
# Remove numbers from string 
table = str.maketrans('', '', digits)
modified_string = string.translate(table)

# Example 3: Using the re.sub() method
# Remove numbers from string 
modified_string = re.sub(r'[0-9]+', '', string)

# Example 4: Using the re.sub() method
pattern_order = r'[0-9]'
char_str = re.sub(pattern_order, '', string)

# Example 5: Using filter() method
# Remove numbers from the string 
modified_string = ''.join(filter(lambda z: not z.isdigit(), string))

# Example 6: Using join and isalpha
# To remove numeric digits from string
modified_string = "".join(x for x in string if x.isalpha())

# Example 7: Using replace() method
# Remove numbers from string 
for i in "0123456789":
    string=string.replace(i,"")

2. Remove Numbers From String Using join() and isdigit() Methods

You can use the join() and isdigit() methods to remove the numbers from the string. The process involves iterating through the characters of the string and selecting only the non-digit characters. Then, you can join those characters back together to form the modified string.

In the below example, the list comprehension iterates through each character in the input string. It checks whether the character is a digit using the isdigit() method. If the character is not a digit (i.e., it’s an alphabet or a special character), it is added to the modified_string. Consequently, the numbers are removed from the original string.


# Initialize the string
string = "welcome2 to34 sparkby56examples01"
print("Original string:",string)

# Using join() and isdigit() methods
# Remove numbers from string 
modified_string = ''.join([i for i in string if not i.isdigit()])
print("After removing numbers from the string:",modified_string)

Yields below output.

python string remove numbers

The output will be “welcome to sparkbyexamples” where all the numbers have been successfully removed from the original string

3. Remove Numbers from the String Using the translate() Method

To remove digits from the string use the translate() method. Before going to use this function we need to import the digits constant from the string module. It contains a string of all the digits (0-9).

In the below example, first, initialize the string variable with the original string you want to process, "welcome2 to34 sparkby56examples01". Create a translation table using the str.maketrans() method. This method generates a mapping table to be used by the translate() method. In this case, it generates a table that maps each digit to None, effectively removing them. Apply the translation table to the original string using the translate() method and store the result in the modified_string variable.


from string import digits

# Initialize the string
string = "welcome2 to34 sparkby56examples01"
print("Original string:",string)

# Using the translate() method
# Remove numbers from string 
table = str.maketrans('', '', digits)
modified_string = string.translate(table)
print("After removing numbers from the string:",modified_string)

Yields the same output as above.

4. Remove Numbers from the String Using the re.sub() Method

Alternatively, you can use the re.sub() method to remove numbers from the string. If you want to work with this function, you need to import the re module in Python, which provides support for regular expressions.

In the below example, first, initialize the string variable with the original string you want to process, "welcome2 to34 sparkby56examples01". Use the re.sub() method to perform the removal of numbers from the string. The regular expression pattern [0-9]+ matches one or more digits (0-9). The + quantifier means “one or more occurrences of the preceding pattern. The re.sub() method replaces all occurrences of the matched pattern (digits) with an empty string '', effectively removing them from the original string. Store the modified string in the modified_string variable.


import re

# Initialize the string
string = "welcome2 to34 sparkby56examples01"
print("Original string:",string)

# Using the re.sub() method
# Remove numbers from string 
modified_string = re.sub(r'[0-9]+', '', string)
print("After removing numbers from the string:",modified_string)

# Using the re.sub() method
pattern_order = r'[0-9]'
char_str = re.sub(pattern_order, '', string)
print("After removing numbers from the string:",modified_string)

Yields the same output as above.

5. Remove Numbers from the String Using filter() Method

To remove numbers from the string use the filter() method. Apply this function over the string and check whether each character in the string is a digit or not. If the character is a digit, it can filter those digits from the given string.

In the below example, first, initialize the string variable with the original string you want to process, "welcome2 to34 sparkby56examples01". Use the filter() method with a lambda function to apply the filter directly to each character in the string. The lambda function lambda z: not z.isdigit() takes a character z as input and returns True if the character is not a digit (i.e., it’s not one of ‘0’, ‘1’, …, ‘9’), and False otherwise. The filter() function returns an iterator containing only the characters for which the lambda function returns True. Convert the filtered iterator back to a string using ''.join(), effectively removing the digits from the original string. Store the modified string in the modified_string variable.


# Initialize the string
string = "welcome2 to34 sparkby56examples01"
print("Original string:",string)

# Using filter() method
# Remove numbers from string 
modified_string = ''.join(filter(lambda z: not z.isdigit(), string))
print("After removing numbers from the string:",modified_string)

Yields the same output as above.

6. Remove Numbers from the String Using join() and isalpha()

Another method is to remove numbers from a string using the join() method and isalpha() function in Python. For example, first initialize a string called string with some alphanumeric characters, including numbers.

The new string is modified_string created using a list comprehension. The isalpha() method is used to check whether each character in the original string is an alphabetical character (a-z or A-Z). The join() method then concatenates all the alphabetical characters from the list to form the modified string. Finally, the code prints the modified string after removing all the numeric digits.


# Initialize the string
string = "welcome2 to34 sparkby56examples01"
print("Original string:",string)

# Using join and isalpha
# To remove numeric digits from string
modified_string = "".join(x for x in string if x.isalpha())
print("After removing numbers from the string:",modified_string)

Yields the same output as above.

7. Remove Numbers from the String Using replace() Method

You can also use replace()method in Python to remove numbers from a given string. For example, first, initialize a string called string with some alphanumeric characters, including numbers. The for loop iterates through each digit (0 to 9) in the string “0123456789”. For each digit, the replace() method is used to replace occurrences of that digit in the original string with an empty string, effectively removing the digit from the string. Finally, the code prints the modified string after removing all the numbers.


# Initialize the string
string = "welcome2 to34 sparkby56examples01"
print("Original string:",string)

# Using replace() method
# Remove numbers from string 
for i in "0123456789":
    string=string.replace(i,"")
print("After removing numbers from the string:",string)

Yields the same output as above.

Conclusion

In this article, I have explained how to remove numbers from the string in Python by using join() & isdigit(), translate(), re.sub(), filter(), join() & isalpha(), and replace() functions with examples

Happy Learning !!