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 = 'neveropen 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 : neveropen 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 = 'neveropen 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 : neveropen 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)
Method #3: Using map() + join() + enumerate() methods and lambda to replace characters in string
Step by step Algorithm:
- Initialize input string, list of indices to replace, and replacement character.
- Use the enumerate() function to get a tuple of index and character for each character in the input string.
- Use the map() function to iterate over each tuple and apply the lambda function.
- In the lambda function, check if the index of the character is in the list of indices to replace. If yes, replace the character with the replacement character. If no, keep the character as is.
- Join the resulting characters using the join() function to get the final string.
- Return the final string.
Python3
test_str = 'neveropen is best' test_list = [ 2 , 4 , 7 , 10 ] repl_char = '*' res = ''.join( map ( lambda x: repl_char if x[ 0 ] in test_list else x[ 1 ], enumerate (test_str))) print ( "The String after performing replace : " + str (res)) |
The String after performing replace : ge*k*fo*ge*ks is best
Time complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n), where n is the length of the input string. This is because we are creating a new string object to store the final result.
Method #4: Using replace() method inside a loop
Step-by-step explanation:
- Initialize the string test_str, the list test_list, and the replacement character repl_char.
- Print the original string.
- Loop through the elements of test_list.
- For each element i in test_list, we replace the character at the i-th position in test_str with repl_char. We achieve this by slicing the string into two parts: the part before the i-th character, and the part after the i-th character then concatenate the repl_char between these two parts.
- We print the resulting string.
Python3
# initializing string test_str = 'neveropen 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 replace() inside a loop for i in test_list: test_str = test_str[:i] + repl_char + test_str[i + 1 :] # printing result print ( "The String after performing replace : " + str (test_str)) |
The original string is : neveropen is best The String after performing replace : ge*k*fo*ge*ks is best
Time complexity: O(n*m), where n is the length of the string and m is the length of the list.
Auxiliary space: O(n), where n is the length of the string. This is because we are modifying the original string in-place, and not creating any new data structures.