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()
Python3
# Python code to demonstrate# how to remove numeric digits from string# using join and isdigit# initialising stringini_string = "Geeks123for127geeks"# printing initial ini_stringprint("initial string : ", ini_string)# using join and isdigit# to remove numeric digits from stringres = ''.join([i for i in ini_string if not i.isdigit()])# printing resultprint("final string : ", res) |
initial string : Geeks123for127geeks final string : Geeksforgeeks
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
Method #2: Using translate and digits
Python3
# Python code to demonstrate# how to remove numeric digits from string# using translatefrom string import digits# initialising stringini_string = "Geeks123for127geeks"# printing initial ini_stringprint("initial string : ", ini_string)# using translate and digits# to remove numeric digits from stringremove_digits = str.maketrans('', '', digits)res = ini_string.translate(remove_digits)# printing resultprint("final string : ", res) |
initial string : Geeks123for127geeks final string : Geeksforgeeks
Method #3: Using filter and lambda
Python3
# Python code to demonstrate# how to remove numeric digits from string# using filter and lambda# initialising stringini_string = "akshat123garg"# printing initial ini_stringprint("initial string : ", ini_string)# using filter and lambda# to remove numeric digits from stringres = "".join(filter(lambda x: not x.isdigit(), ini_string))# res = ini_string# printing resultprint("final string : ", str(res)) |
initial string : akshat123garg final string : akshatgarg
Method#4 Using join() and isalpha()
Python3
# Python code to demonstrate# how to remove numeric digits from string# using join and isalpha# initialising stringstr1 = "Geeks123for127geeks"# printing initial ini_stringprint("initial string : ", str1)# using join and isaplha# to remove numeric digits from stringstr2 = "".join(x for x in str1 if x.isalpha())# printing resultprint("final string : ", str2) |
initial string : Geeks123for127geeks final string : Geeksforgeeks
Method#5: Using loop and in
Python3
# Python code to demonstrate# how to remove numeric digits from string# using loop and in# initialising stringstr1 = "Geeks123for127geeks"# printing initial ini_stringprint("initial string : ", str1)# using loop and in# to remove numeric digits from stringnum = "1234567890"str2 = ""for i in str1: if i not in num: str2+=i# printing resultprint("final string : ", str2) |
initial string : Geeks123for127geeks final string : Geeksforgeeks
Method #6: Using ord() function
Python3
# Python code to demonstrate# how to remove numeric digits from string# using loop and in# initialising stringstr1 = "Geeks123for127geeks"# printing initial ini_stringprint("initial string : ", str1)# using loop and in# to remove numeric digits from stringstr2=""for i in str1: if(not ord(i) in range(48,58)): str2+=i# printing resultprint("final string : ", str2) |
initial string : Geeks123for127geeks final string : Geeksforgeeks
Method #7: Using re
One approach to remove numeric digits from a given string is to use a regular expression to match and remove the digits. This can be done using the re module in Python.
Here is an example of how this can be done:
Python3
import re# Initialize the stringini_string = "Geeks123for127geeks"# Print the initial stringprint("initial string : ", ini_string)# Use a regular expression to remove the digitsres = re.sub(r'\d', '', ini_string)# Print the resultprint("final string : ", res)#This code is contributed by Edula Vinay Kumar Reddy |
initial string : Geeks123for127geeks final string : Geeksforgeeks
This method has a time complexity of O(n), where n is the length of the string, as it needs to iterate through the entire string to remove the digits. The space complexity is O(n), as it only requires memory to store the result.
Method #8: Using replace() method
Python3
# Python code to demonstrate# how to remove numeric digits from string# initialising stringini_string = "Geeks123for127geeks"# printing initial ini_stringprint("initial string : ", ini_string)# to remove numeric digits from stringfor i in "0123456789": ini_string=ini_string.replace(i,"") # printing resultprint("final string : ", ini_string) |
initial string : Geeks123for127geeks final string : Geeksforgeeks
This method has a time complexity of O(n), where n is the length of the string, as it needs to iterate through the entire string to remove the digits. The auxiliary space is O(n), as it only requires memory to store the result.






Please Login to comment...