Python | Ways to remove numeric digits from given string
Last Updated :
21 Apr, 2023
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
ini_string = "Geeks123for127geeks"
print ( "initial string : " , ini_string)
res = ''.join([i for i in ini_string if not i.isdigit()])
print ( "final string : " , res)
|
Output
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
from string import digits
ini_string = "Geeks123for127geeks"
print ( "initial string : " , ini_string)
remove_digits = str .maketrans(' ', ' ', digits)
res = ini_string.translate(remove_digits)
print ( "final string : " , res)
|
Output
initial string : Geeks123for127geeks
final string : Geeksforgeeks
Time Complexity: O(n), where n is the length of the input string “ini_string”. The reason is that the translate function is applied only once to the input string “ini_string”.
Auxiliary Space: O(1), as it only uses a constant amount of extra memory to store the removed digits and the result string.
Method #3: Using filter and lambda
Python3
ini_string = "akshat123garg"
print ( "initial string : " , ini_string)
res = "".join( filter ( lambda x: not x.isdigit(), ini_string))
print ( "final string : " , str (res))
|
Output
initial string : akshat123garg
final string : akshatgarg
Time Complexity: O(n), where n is the length of the initial string “ini_string”.
Auxiliary Space: O(n), as the filter function creates a new string “res” which is of length n. The variable “res” is stored in memory, hence the space complexity is O(n).
Method#4 Using join() and isalpha()
Python3
str1 = "Geeks123for127geeks"
print ( "initial string : " , str1)
str2 = "".join(x for x in str1 if x.isalpha())
print ( "final string : " , str2)
|
Output
initial string : Geeks123for127geeks
final string : Geeksforgeeks
Time Complexity: O(n), where n is the length of the string. This is because the code loops through each character in the string and performs a simple check for each character.
Auxiliary Space: O(n), where n is the length of the final string after removing numeric digits. This is because a new string of size n is created as the result of removing numeric digits from the original string.
Method#5: Using loop and in
Python3
str1 = "Geeks123for127geeks"
print ( "initial string : " , str1)
num = "1234567890"
str2 = ""
for i in str1:
if i not in num:
str2 + = i
print ( "final string : " , str2)
|
Output
initial string : Geeks123for127geeks
final string : Geeksforgeeks
Time complexity: O(n), where n is the length of the string. The loop iterates through each character of the string once.
Auxiliary space complexity: O(n), where n is the length of the string. The str2 variable is created to store the final string without numeric digits, which is the same length as the original string.
Method #6: Using ord() function
Python3
str1 = "Geeks123for127geeks"
print ( "initial string : " , str1)
str2 = ""
for i in str1:
if ( not ord (i) in range ( 48 , 58 )):
str2 + = i
print ( "final string : " , str2)
|
Output
initial string : Geeks123for127geeks
final string : Geeksforgeeks
Time complexity: O(n), where n is the length of the input string ‘str1’.
Auxiliary space: O(n), where n is the length of the input string ‘str1’.
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
ini_string = "Geeks123for127geeks"
print ( "initial string : " , ini_string)
res = re.sub(r '\d' , '', ini_string)
print ( "final string : " , res)
|
Output
initial string : Geeks123for127geeks
final string : Geeksforgeeks
Time Complexity: O(n), where n is the length of the string, as it needs to iterate through the entire string to remove the digits.
Auxiliary Space: O(n), as it only requires memory to store the result.
Method #8: Using replace() method
Python3
ini_string = "Geeks123for127geeks"
print ( "initial string : " , ini_string)
for i in "0123456789" :
ini_string = ini_string.replace(i,"")
print ( "final string : " , ini_string)
|
Output
initial string : Geeks123for127geeks
final string : Geeksforgeeks
Time Complexity: O(n), where n is the length of the string, as it needs to iterate through the entire string to remove the digits.
Auxiliary Space: O(n), as it only requires memory to store the result.
Method #9 : Using re.match()
Approach
- Initiate a for loop to traverse ini_string, create an empty string res
- Append the characters which are not numeric to res (to check numeric or not use regular expression ‘^[0-9]+$’ and re.match() method)
- Display res
Python3
import re
ini_string = "Geeks123for127geeks"
print ( "initial string : " , ini_string)
res = ""
for i in ini_string:
if not re.match( '^[0-9]+$' , i):
res + = i
print ( "final string : " , res)
|
Output
initial string : Geeks123for127geeks
final string : Geeksforgeeks
Time Complexity: O(n), where n is the length of the string, as it needs to iterate through the entire string to remove the digits.
Auxiliary Space: O(n), as it only requires memory to store the result.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...