Python | Multiple indices Replace in String
Sometimes, while working with Python Stings, we can have a problem, in which we need to perform the replace of characters based on several indices of String. This kind of problem can have applications in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + join() This is brute force way in which this task can be performed. In this, we iterate for each character and substitute with replace character if that is one.
Python3
# Python3 code to demonstrate working of # Multiple indices Replace in String # Using loop + join() # initializing string test_str = 'geeksforgeeks is best' # printing original string print ( "The original string is : " + test_str) # initializing list test_list = [ 2 , 4 , 7 , 10 ] # initializing repl char repl_char = '*' # Multiple indices Replace in String # Using loop + join() temp = list (test_str) for idx in test_list: temp[idx] = repl_char res = ''.join(temp) # printing result print ( "The String after performing replace : " + str (res)) |
The original string is : geeksforgeeks is best The String after performing replace : ge*k*fo*ge*ks is best
Method #2 : Using list comprehension + join() The combination of above functions can also be used to perform this task. In this, we perform similar task as above, just in one liner format using list comprehension.
Python3
# Python3 code to demonstrate working of # Multiple indices Replace in String # Using list comprehension + join() # initializing string test_str = 'geeksforgeeks is best' # printing original string print ( "The original string is : " + test_str) # initializing list test_list = [ 2 , 4 , 7 , 10 ] # initializing repl char repl_char = '*' # Multiple indices Replace in String # Using list comprehension + join() temp = list (test_str) res = [repl_char if idx in test_list else ele for idx, ele in enumerate (temp)] res = ''.join(res) # printing result print ( "The String after performing replace : " + str (res)) |
The original string is : geeksforgeeks is best The String after performing replace : ge*k*fo*ge*ks is best
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Please Login to comment...