Python | Ways to remove numeric digits from given string
Given a string (may contain both characters and digits), write a Python program to remove the numeric digits from string.
Let’s discuss the different ways we can achieve this task.
Method #1: Using join and isdigit()
# Python code to demonstrate # how to remove numeric digits from string # using join and isdigit # initialising string ini_string = "Geeks123for127geeks" # printing initial ini_string print("initial string : ", ini_string) # using join and isdigit # to remove numeric digits from string res = ''.join([i for i in ini_string if not i.isdigit()]) # printing result print("final string : ", res) |
chevron_right
filter_none
Output:
initial string : Geeks123for127geeks final string : Geeksforgeeks
Method #2: Using translate and digits
# Python code to demonstrate # how to remove numeric digits from string # using translate from string import digits # initialising string ini_string = "Geeks123for127geeks" # printing initial ini_string print("initial string : ", ini_string) # using translate and digits # to remove numeric digits from string remove_digits = str.maketrans('', '', digits) res = ini_string.translate(remove_digits) # printing result print("final string : ", res) |
chevron_right
filter_none
Output:
initial string : Geeks123for127geeks final string : Geeksforgeeks
Method #3: Using filter and lambda
# Python code to demonstrate # how to remove numeric digits from string # using filter and lambda # initialising string ini_string = "akshat123garg" # printing initial ini_string print("initial string : ", ini_string) # using filter and lambda # to remove numeric digits from string res = "".join(filter(lambda x: not x.isdigit(), ini_string)) # res = ini_string # printing result print("final string : ", str(res)) |
chevron_right
filter_none
Output:
initial string : akshat123garg final string : akshatgarg
Recommended Posts:
- Ways to remove i'th character from string in Python
- Python | Ways to remove n characters from start of given string
- Python | Get the numeric prefix of given string
- Python | Check if given string is numeric or not
- Python | Get numeric prefix of given string
- Python | Check Numeric Suffix in String
- Python | Ways to remove a key from dictionary
- Python Regex to extract maximum numeric value from a string
- Python | Ways to remove particular list element
- Python | Ways to remove duplicates from list
- Python | Remove all digits from a list of strings
- Python | Remove element from given list containing specific digits
- Reverse string in Python (5 different ways)
- Python | Ways to find all permutation of a string
- Python | Ways to check string contain all same characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



