Given a string and a character, write a Python program to replace multiple occurrences of the given character by a single character.
Input : Geeksforgeeks, ch = 'e' Output : Geksforgeks Input : Wiiiin, ch = 'i' Output : Win
Approach #1 : Naive Approach
This method is a brute force approach in which we take another list ‘new_str’. Use a for loop to check if the given character is repeated or not. If repeated multiple times, append the character single time to the list. Other characters(Not the given character) are simply appended to the list without any alteration.
Approach #2 : Using Python Regex
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Python - Replace duplicate Occurrence in String
- replace() in Python to replace a substring
- Python | Pandas Series.str.replace() to replace text in a series
- Generate two output strings depending upon occurrence of character in input string in Python
- Python program to find occurrence to each character in given string
- Python | Split string on Kth Occurrence of Character
- Python | First character occurrence from rear String
- Python | Substitute character with its occurrence
- Python | Replace multiple characters at once
- Python | Multiple indices Replace in String
- Python - Replace multiple words with K
- Python - Replace K with Multiple values
- Print Single and Multiple variable in Python
- Python | Convert a list of multiple integers into a single integer
- Python program to Replace all Characters of a List Except the given character
- Python | Convert Character Matrix to single String
- Combine Multiple Excel Worksheets Into a Single Pandas Dataframe
- Replace missing white spaces in a string with the least frequent character using Pandas
- Python | Insert character after every character pair
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.