Multiple Indices Replace in String - Python Last Updated : 17 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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. Python 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) OutputThe 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. Python 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) OutputThe String after performing replace: ge*k*fo*ge*ks is best Explanation:Using list comprehension we check each character's index (idx) in temp, if the index is in li then it replaces the character with ch.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. Python 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) OutputThe String after performing replace : ge*k*fo*ge*ks is bestExplanation: 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 LoopIn this method we use the replace() method within a loop to replace characters at the specified indices. Python 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) OutputThe 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 with ch 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. Comment More infoAdvertise with us Next Article Multiple Indices Replace in String - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Replace Multiple Lines From A File Using Python In Python, replacing multiple lines in a file consists of updating specific contents within a text file. This can be done using various modules and their associated functions. In this article, we will explore three different approaches along with the practical implementation of each approach in term 3 min read Case insensitive string replacement in Python We are given a string and our task is to replace a specific word in it, ignoring the case of the letters. This means if we want to replace the word "best" with "good", then all variations like "BeSt", "BEST", or "Best" should also be replaced. For example, if the input is "gfg is BeSt", then the out 3 min read Replace multiple words with K - Python We are given a string s and the task is to replace all occurrences of specified target words with a single replacement word K.For example, given text = "apple orange banana" and words_to_replace = ["apple", "banana"], the output will be "K orange K".Using List ComprehensionThis is the most efficient 4 min read Python - Replace K with Multiple values Sometimes, while working with Python Strings, we can have a problem in which we need to perform replace of single character/work with particular list of values, based on occurrence. This kind of problem can have application in school and day-day programming. Let's discuss certain ways in which this 5 min read Remove Multiple Characters from a String in Python Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det 2 min read In-Place String Modifications in Python Strings are immutable in Python, meaning their values cannot be modified directly after creation. However, we can simulate in-place string modifications using techniques such as string slicing and conversion to mutable data types like lists.Using String SlicingString slicing is one of the most effic 2 min read Python - Replace all numbers by K in given String We need to write a python program to replace all numeric digits (0-9) in a given string with the character 'K', while keeping all non-numeric characters unchanged. For example we are given a string s="hello123world456" we need to replace all numbers by K in the given string so the string becomes "he 2 min read Python - Replace multiple characters at once Replacing multiple characters in a string is a common task in Python Below, we explore methods to replace multiple characters at once, ranked from the most efficient to the least.Using translate() with maketrans() translate() method combined with maketrans() is the most efficient way to replace mult 2 min read Replace substring in list of strings - Python We are given a list of strings, and our task is to replace a specific substring within each string with a new substring. This is useful when modifying text data in bulk. For example, given a = ["hello world", "world of code", "worldwide"], replacing "world" with "universe" should result in ["hello u 3 min read Python - Retain Numbers in String Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for 2 min read Like