Open In App

Python – Replace all numbers by K in given String

Last Updated : 01 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Replace numbers by K in String
# Using replace() + isdigit()
 
# initializing string
test_str = 'G4G is 4 all No. 1 Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '@'
 
# loop for all characters
for ele in test_str:
    if ele.isdigit():
        test_str = test_str.replace(ele, K)
         
# printing result
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




# Python3 code to demonstrate working of
# Replace numbers by K in String
# Using regex() + sub()
import re
 
# initializing string
test_str = 'G4G is 4 all No. 1 Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '@'
 
# using regex expression to solve problem
res = re.sub(r'\d', K, test_str)
 
# printing result
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




# Python3 code to demonstrate working of
# Replace numbers by K in String
# Using replace()
 
# initializing string
test_str = 'G4G is 4 all No. 1 Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '@'
digits = "0123456789"
 
# loop for all characters
for ele in test_str:
    if ele in digits:
        test_str = test_str.replace(ele, K)
 
# printing result
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:

  1. Initiate a for loop over the string.
  2. Check for the ASCII values of characters if the ASCII values fall in the range of 48-57 then the characters are numeric
  3. Replace all those characters with K.

Python3




# Python3 code to demonstrate working of
# Replace numbers by K in String
# Using replace() and ord()
 
# initializing string
test_str = 'G4G is 4 all No. 1 Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '@'
 
digs = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
 
# loop for all characters
for ele in test_str:
    if ord(ele) in digs:
        test_str = test_str.replace(ele, K)
 
# printing result
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




# Python3 code to demonstrate working of
# Replace numbers by K in String
# Using list comprehension and join()
 
# initializing string
test_str = 'G4G is 4 all No. 1 Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '@'
 
# Replacing digits with K
# using list comprehension
test_str = ''.join([K if ele.isdigit() else ele for ele in test_str])
 
# printing result
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 

  1. The program initializes a string variable test_str with the value ‘G4G is 4 all No. 1 Geeks’.
  2. The program prints the original string using the print() function and string concatenation.
  3. The program initializes a variable K with the value ‘@’.
  4. The program initializes an empty string variable new_str.
  5. The program enters a for loop, which iterates over each character in the string test_str.
  6. For each character, the program checks whether it is a digit using the isdigit() method.
  7. If the character is a digit, the program appends the character K to the new_str variable.
  8. Otherwise, the program appends the current character to the new_str variable.
  9. After iterating over all the characters in test_str, the program prints the resulting string using the print() function and string concatenation.

Python3




# Python3 code to demonstrate working of
# Replace numbers by K in String
# Using for loop and string concatenation
 
# initializing string
test_str = 'G4G is 4 all No. 1 Geeks'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing K
K = '@'
 
# using for loop to replace digits with K
new_str = ''
for char in test_str:
    if char.isdigit():
        new_str += K
    else:
        new_str += char
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads