Movatterモバイル変換


[0]ホーム

URL:


Open In App

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:

Usingloop andjoin()

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 replacech='*'# Replacement charactertemp=list(s)# Replace characters at specified indicesforiinli:temp[i]=chres=''.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. 

Python
s="geeksforgeeks is best"li=[2,4,7,10]# Indices to replacech='*'# Replacement charactertemp=list(s)res=[chifidxinlielseeleforidx,eleinenumerate(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, if 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 usemap()withlambdaandenumerate() to replace characters at specific indices in the string. where theenumerate() function provides both the index and the character and themap() 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 replacech='*'# Replacement characterres=''.join(map(lambdax:chifx[0]inlielsex[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 thelambda function checks if the index is inli and if it is then it replaces the character withch, otherwise it keeps the original character.

Using String Slicing and Concatenation in a Loop

In this method we use thereplace() method within a loop to replace characters at the specified indices.

Python
s="geeksforgeeks is best"li=[2,4,7,10]# Indices to replacech='*'# Replacement character# Loop through each index in li and replace the character at that index with 'ch'foriinli: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 indexi 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 inli and for each index the string is updated by replacing the character at that specific index.

Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp