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 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) |
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 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) |
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 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)) |
Output:
initial string : akshat123garg final string : akshatgarg


.png)