Python | Ways to remove numeric digits from given string
Last Updated :
30 Dec, 2024
In Python, we can remove numeric digits from a string in multiple ways. For example, if we have a string like "Geeks123" and we want to remove the numbers to get "Geeks", we can use different methods to accomplish this. We can do this by using techniques such as list comprehension, regular expressions, string methods and filters.
Using Regular Expressions (re.sub())
Regular expressions (regex) in Python that allow us to search for patterns in text. In our case, we can use regex to find all the digits in the string and replace them with an empty string. The re.sub() function is used to search for a pattern in a string and replace it. In the pattern \d+, \d means "any digit" and + means "one or more". So \d+ matches all digits in the string.
Python
import re
s = "geeks123"
result = re.sub(r'\d+', '', s) # Remove all digits from the string
print(result)
Other methods that we can use to remove numeric digits from given string:
Using str.isalpha() with Loop
With this approach we iterate through each character in the string and check if it’s an alphabet letter. isalpha() method returns True if the character is a letter and False if it is not.
Python
s = "geeks123"
result = ""
for char in s:
if char.isalpha(): # Check if the character is not a number
result += char # Add it to result if it's a letter
print(result)
Using List Comprehension
List comprehension is a more concise way to achieve the same result as the loop. It combines the loop and the condition into a single line of code.
Python
s = "geeks123"
result = ''.join([char for char in s if char.isalpha()])
print(result)
Explanation:
- The code uses a list comprehension to iterate over each character in the string s, adding only alphabetic characters (using char.isalpha()) to a new list, which is then joined into a single string using ''.join(). The result is "geeks".
Using filter() Function
filter() function is a built-in Python function that can be used to filter out certain elements from an iterable, like a string. In our case, we can filter out all characters that are not alphabetic. The filter() function takes two arguments: a function and an iterable. We use str.isalpha as the function, which checks whether a character is alphabetic. filter() will only keep the characters that pass the isalpha() check and remove the rest.
Python
s = "geeks12332"
result = ''.join(filter(str.isalpha, s)) # Filter out non-letter characters
print(result)
Similar Reads
Python | Remove all digits from a list of strings The problem is about removing all numeric digits from each string in a given list of strings. We are provided with a list where each element is a string and the task is to remove any digits (0-9) from each string, leaving only the non-digit characters. In this article, we'll explore multiple methods
4 min read
Python - Remove digits from Dictionary String Values List We are given dictionary we need to remove digits from the dictionary string value list. For example, we are given a dictionary that contains strings values d = {'key1': ['abc1', 'def2', 'ghi3'], 'key2': ['xyz4', 'lmn5']} we need to remove digits from dictionary so that resultant output becomes {'key
3 min read
Python - Remove numbers with repeating digits Given a list of numbers, the task is to write a Python program to remove all numbers with repetitive digits. Examples: Input : test_list = [4252, 6578, 3421, 6545, 6676]Output : test_list = [6578, 3421]Explanation : 4252 has 2 occurrences of 2 hence removed. Similar case for all other removed. Input
5 min read
Python - Extract digits from given string We need to extract the digit from the given string. For example we are given a string s=""abc123def456gh789" we need to extract all the numbers from the string so the output for the given string will become "123456789" In this article we will show different ways to extract digits from a string in Py
2 min read
Python Program for Remove leading zeros from a Number given as a string Given numeric string str, the task is to remove all the leading zeros from a given string. If the string contains only zeros, then print a single "0".Examples:Input: str = "0001234" Output: 1234 Explanation: Removal of leading substring "000" modifies the string to "1234". Hence, the final answer is
3 min read