Python | Replacing Nth occurrence of multiple characters in a String with the given character - GeeksforGeeks

14 May, 2021
Given a String S, a character array ch[], a number N and a replacing character, the task is to replace every Nth occurrence of each character of the character array ch[] in the string with the given replacing character. 
Input: S = “GeeksforGeeks”, ch[] = {‘G’, ‘e’, ‘k’}, N = 2, replacing_character = ‘#’ Output: Ge#ksfor#ee#s Explanation: In the given string S, the second occurrence of the ‘G’, ‘e’, ‘K’ is replaced with ‘#’  
Input: S = abcdeahu, ch[] = {‘a’, ‘d’, ‘u’}, N = 1, replacing_character = ‘#’ Output: #bc#eah# Explanation: In the given string S, the first occurrence of the ‘a’, ‘d’, ‘u’ is replaced with ‘#’  
Method 1: Naive approach In this approach, the general idea is to store every Nth Occurrence index of every character in another array and replacing them with the given another character.
Below is the implementation of the above approach. 
Python
# Python implementation to replace nth# occurrence of the every character# in a string # Function to replace the Nth occurrence# of the character of stringdef replacer(initial_string, ch,         replacing_character, occurrence):         # breaking a string into it's    # every single character in list    lst1 = list(initial_string)    lst2 = list(ch)         # List to store the indexes in which    # it is replaced with the    # replacing_character    lst3 = []         # Loop to find the Nth occurrence of    # given characters in the string    for i in lst2:        if(lst1.count(i)>= occurrence):            count = 0            for j in range(0, len(initial_string)):                if(i == lst1[j]):                    count+= 1                    if(count == occurrence):                        lst3.append(j)         for i in lst3:        # Replacing that particular index        # with the requested character        lst1[i] = replacing_character                  print(''.join(lst1))     # Driver Code:if __name__ == '__main__':    initial_string = 'GeeksforGeeks'    ch = ['G', 'e', 'k']    occurrence = 2    replacing_character = '#'    replacer(initial_string, ch,    replacing_character, occurrence)
Ge#ksfor#ee#s
 
Method 2: Using find() method In this approach, the idea is to use the find() function to find the Nth occurrence of the character in the given string S and replace it with another giver character.
Below is the implementation of the above approach.
Python
# Python implementation to replace nth# occurrence of the every character# in a string using find() function # Function to replace the Nth occurrence# of the character of stringdef replacer(initial_string, ch,         replacing_character, occurrence):         # breaking a string into    # it's every single character    lst1 = list(initial_string)    lst2 = list(ch)     # Loop to find the occurrence    # of the character in the string    # and replace it with the given    # replacing_character    for i in lst2:        sub_string = i        val = -1        for i in range(0, occurrence):            val = initial_string.find(sub_string,                                      val + 1)        lst1[val] = replacing_character     print(''.join(lst1)) # Driver Code:if __name__ == '__main__':    initial_string = 'GeeksforGeeks'    ch = ['G', 'e', 'k']    occurrence = 2    replacing_character = '#'    replacer(initial_string, ch,    replacing_character, occurrence)
Ge#ksfor#ee#s
 
Method 3: Using startswith() method In this approach, the idea is to use the startswith() function of the python to find the index of the character where the occurrence of the character is equal to the given Nth occurrence and then replace with the given replacing character.
Below is the implementation of the above approach. 
Python
# Python implementation to replace nth# occurrence of the every character# in a string # Function to replace the Nth occurrence# of the character of stringdef replacer(initial_string, ch,         replacing_character, occurrence):         # breaking a string into    # it's every single character    lst1 = list(initial_string)    lst2 = list(ch)         # Loop to find the occurrence    # of the character in the string    # and replace it with the given    # replacing_character       for j in lst2:        sub_string = j        checklist = [i for i in range(0, len(initial_string))              if initial_string[i:].startswith(sub_string)]        if len(checklist)>= occurrence:            lst1[checklist[occurrence-1]] = replacing_character     print(''.join(lst1)) # Driver Code:if __name__ == '__main__':    initial_string = 'GeeksforGeeks'    ch = ['G', 'e', 'k']    occurrence = 2    replacing_character = '#'    replacer(initial_string, ch,    replacing_character, occurrence)
Ge#ksfor#ee#s
 
simmytarika5
Python-array
python-list
python-list-functions
Python
Strings
python-list
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4