Python – Replace all numbers by K in given String
Last Updated :
01 May, 2023
Given a string containing numbers, replace each number by K.
Input : test_str = ‘G4G is 4 all No. 1 Geeks’, K = ‘#’
Output : G#G is # all No. # Geeks
Explanation : All numbers replaced by K.
Input : test_str = ‘G4G is 4 all No. Geeks’, K = ‘#’
Output : G#G is # all No. Geeks
Explanation : All numbers replaced by K.
Method #1 : Using replace() + isdigit()
In this, we check for numerics using isdigit(), and replace() is used to perform the task of replacing the numbers by K.
Python3
test_str = 'G4G is 4 all No. 1 Geeks'
print ( "The original string is : " + str (test_str))
K = '@'
for ele in test_str:
if ele.isdigit():
test_str = test_str.replace(ele, K)
print ( "The resultant string : " + str (test_str))
|
Output
The original string is : G4G is 4 all No. 1 Geeks
The resultant string : G@G is @ all No. @ Geeks
Time complexity : O(n)
Auxiliary Space: O(n)
Method #2 : Using regex() + sub()
In this, appropriate regex is used to identify digits, and sub() is used to perform replace.
Python3
import re
test_str = 'G4G is 4 all No. 1 Geeks'
print ( "The original string is : " + str (test_str))
K = '@'
res = re.sub(r '\d' , K, test_str)
print ( "The resultant string : " + str (res))
|
Output
The original string is : G4G is 4 all No. 1 Geeks
The resultant string : G@G is @ all No. @ Geeks
Time complexity : O(n)
Auxiliary Space: O(n)
Method #3: Without using inbuilt functions
Python3
test_str = 'G4G is 4 all No. 1 Geeks'
print ( "The original string is : " + str (test_str))
K = '@'
digits = "0123456789"
for ele in test_str:
if ele in digits:
test_str = test_str.replace(ele, K)
print ( "The resultant string : " + str (test_str))
|
Output
The original string is : G4G is 4 all No. 1 Geeks
The resultant string : G@G is @ all No. @ Geeks
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using ord() and replace() methods
Approach:
- Initiate a for loop over the string.
- Check for the ASCII values of characters if the ASCII values fall in the range of 48-57 then the characters are numeric
- Replace all those characters with K.
Python3
test_str = 'G4G is 4 all No. 1 Geeks'
print ( "The original string is : " + str (test_str))
K = '@'
digs = [ 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 ]
for ele in test_str:
if ord (ele) in digs:
test_str = test_str.replace(ele, K)
print ( "The resultant string : " + str (test_str))
|
Output
The original string is : G4G is 4 all No. 1 Geeks
The resultant string : G@G is @ all No. @ Geeks
Time Complexity : O(n)
Auxiliary Space : O(n)
Method #5: Using a list comprehension and join() method
Replace all the digits in a string with a given character using a list comprehension and join() method.
Python3
test_str = 'G4G is 4 all No. 1 Geeks'
print ( "The original string is : " + str (test_str))
K = '@'
test_str = ''.join([K if ele.isdigit() else ele for ele in test_str])
print ( "The resultant string : " + str (test_str))
|
Output
The original string is : G4G is 4 all No. 1 Geeks
The resultant string : G@G is @ all No. @ Geeks
Time complexity: O(n), where n is the length of the input string,
Auxiliary space: O(n)
Method 6 : Using a for loop and string concatenation
step-by-step approach
- The program initializes a string variable test_str with the value ‘G4G is 4 all No. 1 Geeks’.
- The program prints the original string using the print() function and string concatenation.
- The program initializes a variable K with the value ‘@’.
- The program initializes an empty string variable new_str.
- The program enters a for loop, which iterates over each character in the string test_str.
- For each character, the program checks whether it is a digit using the isdigit() method.
- If the character is a digit, the program appends the character K to the new_str variable.
- Otherwise, the program appends the current character to the new_str variable.
- After iterating over all the characters in test_str, the program prints the resulting string using the print() function and string concatenation.
Python3
test_str = 'G4G is 4 all No. 1 Geeks'
print ( "The original string is : " + str (test_str))
K = '@'
new_str = ''
for char in test_str:
if char.isdigit():
new_str + = K
else :
new_str + = char
print ( "The resultant string : " + str (new_str))
|
Output
The original string is : G4G is 4 all No. 1 Geeks
The resultant string : G@G is @ all No. @ Geeks
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), to store the new string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...