Python | Multiple indices Replace in String
Last Updated :
16 May, 2023
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
test_str = 'geeksforgeeks is best'
print ( "The original string is : " + test_str)
test_list = [ 2 , 4 , 7 , 10 ]
repl_char = '*'
temp = list (test_str)
for idx in test_list:
temp[idx] = repl_char
res = ''.join(temp)
print ( "The String after performing replace : " + str (res))
|
Output :
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
test_str = 'geeksforgeeks is best'
print ( "The original string is : " + test_str)
test_list = [ 2 , 4 , 7 , 10 ]
repl_char = '*'
temp = list (test_str)
res = [repl_char if idx in test_list else ele for idx, ele in enumerate (temp)]
res = ''.join(res)
print ( "The String after performing replace : " + str (res))
|
Output :
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)
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 = 'geeksforgeeks 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))
|
Output
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
test_str = 'geeksforgeeks is best'
print ( "The original string is : " + test_str)
test_list = [ 2 , 4 , 7 , 10 ]
repl_char = '*'
for i in test_list:
test_str = test_str[:i] + repl_char + test_str[i + 1 :]
print ( "The String after performing replace : " + str (test_str))
|
Output
The original string is : geeksforgeeks 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.
Please Login to comment...