Multiple Indices Replace in String – Python
In this problem, we have to replace the characters in a string at multiple specified indices and the following methods demonstrate how to perform this operation efficiently:
Using loop
and join()
join() is a brute force method where we first convert the string into a list then we iterate through the list replacing characters at specific indices. Finally we join the list back into a string.
s = "geeksforgeeks is best"
li = [2, 4, 7, 10] # Indices to replace
ch = '*' # Replacement character
temp = list(s)
# Replace characters at specified indices
for i in li:
temp[i] = ch
res = ''.join(temp)
print("The String after performing replace:", res)
Output
The String after performing replace: ge*k*fo*ge*ks is best
Using List Comprehension + join()
In this method we use list comprehension to replace characters at the specified indices in a single line, where we create a new list in which each character is checked against the indices list and if it matches then it is replaced with the specified character. Then final list is then joined back into a string.
s = "geeksforgeeks is best"
li = [2, 4, 7, 10] # Indices to replace
ch = '*' # Replacement character
temp = list(s)
res = [ch if idx in li else ele for idx, ele in enumerate(temp)]
res = ''.join(res)
print("The String after performing replace:", res)
Output
The String after performing replace: ge*k*fo*ge*ks is best
Explanation:
- Using list comprehension we check each character’s index (
idx
) intemp, i
f the index is inli then
it replaces the character withch
. - After replacing the necessary characters we use
' '.join()
to combine the list into a final string.
Using map(), join() and enumerate()
In this method we use map()
with lambda
and enumerate()
to replace characters at specific indices in the string. where the enumerate()
function provides both the index and the character and the map()
function applies a replacement condition based on whether the index is in the specified list.
s = "geeksforgeeks is best"
li = [2, 4, 7, 10] # Indices to replace
ch = '*' # Replacement character
res = ''.join(map(lambda x: ch if x[0] in li else x[1], enumerate(s)))
print("The String after performing replace:", res)
Output
The String after performing replace : ge*k*fo*ge*ks is best
Explanation: Here, enumerate(s)
generates index-character pairs and the lambda
function checks if the index is in li and if
it is then it replaces the character with ch
, otherwise it keeps the original character.
Using String Slicing and Concatenation in a Loop
In this method we use the replace()
method within a loop to replace characters at the specified indices.
s = "geeksforgeeks is best"
li = [2, 4, 7, 10] # Indices to replace
ch = '*' # Replacement character
# Loop through each index in li and replace the character at that index with 'ch'
for i in li:
s = s[:i] + ch + s[i+1:]
print("The String after performing replace:", s)
Output
The String after performing replace: ge*k*fo*ge*ks is best
Explanation:
- String is sliced at each specified index
i
and the character at that index is replaced withch
by concatenating the parts before and after the index. - For loop iterates through the indices provided in
li
and for each index the string is updated by replacing the character at that specific index.