How do you strip a number from a string in python?

Here's a method using str.join(), str.isnumeric(), and a generator expression which will work in 3.x:

>>> my_str = '123Hello, World!4567'
>>> output = ''.join(c for c in my_str if not c.isnumeric())
>>> print(output)
Hello, World!
>>> 

This will also work in 2.x, if you use a unicode string:

>>> my_str = u'123Hello, World!4567'
>>> output = ''.join(c for c in my_str if not c.isnumeric())
>>> print(output)
Hello, World!
>>> 

Hmm. Throw in a paperclip and we'd have an episode of MacGyver.

Update

I know that this has been closed out as a duplicate, but here's a method that works for both Python 2 and Python 3:

>>> my_str = '123Hello, World!4567'
>>> output = ''.join(map(lambda c: '' if c in '0123456789' else c, my_str))
>>> print(output)
Hello, World!
>>>

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.

How do I remove a number from a string in Python?

Let's discuss the different ways we can achieve this task..
Method #1: Using join and isdigit().
Method #2: Using translate and digits..
Method #3: Using filter and lambda..
Method#4 Using join() and isalpha().
Method#5: Using loop and in..
Method #6: Using ord() function..

How do you strip a value in Python?

The strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string. Parameter: chars(optional): Character or a set of characters, that needs to be removed from the string. Returns: A copy of the string with both leading and trailing characters stripped.

How do I remove numbers from a string?

Get the String to remove all the digit..
Convert the given string into a character array..
Initialize an empty string that stores the result..
Traverse the character array from start to end..
Check if the specified character is not digit then add this character into the result variable..
Now, print the result..

How do you strip part of a string in Python?

Remove a part of a string in Python.
Remove a substring by replacing it with an empty string. ... .
Remove leading and trailing characters: strip().
Remove leading characters: lstrip().
Remove trailing characters: rstrip().
Remove prefix: removeprefix() (Python 3.9 or later).
Remove suffix: removesuffix() (Python 3.9 or later).