Python | Replace characters after K occurrences

Sometimes, while working with Python strings, we can have problem in which we need to perform replace of characters after certain repetitions of characters. This can have application in many domains including day-day and competitive programming.

Method #1 : Using loop + string slicing
This is brute force way in which this problem can be solved. In this, we run a loop on string and keep track of occurrences and perform replace when above K occurrence.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Replace characters after K occurrences
# Using loop + string slices
  
# initializing string
test_str = "geeksforgeeks is best for geeks"
  
# printing original string
print("The original string is : " + test_str)
  
# initializing K 
K = 2
  
# initializing Repl char
repl_char = "*"
  
# Replace characters after K occurrences
# Using loop + string slices
for sub in set(test_str):
    for idx in [idx for idx in range(len(test_str)) if test_str[idx] == sub][K:]:
        test_str = test_str[:idx] + repl_char + test_str[idx + 1:]
  
# printing result 
print("The string after performing replace : " + test_str) 

chevron_right


Output :

The original string is : geeksforgeeks is best for geeks
The string after performing replace : geeksforg**ks i* b**t*for******

 

Method #2 : Using join() + count() + enumerate()
This is shorthand by which this task can be performed. In this, we employ count() to check for the count of strings and join() and enumerate() can be used to perform the task of new string construction.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Replace characters after K occurrences
# Using join() + count() + enumerate()
  
# initializing string
test_str = "geeksforgeeks is best for geeks"
  
# printing original string
print("The original string is : " + test_str)
  
# initializing K 
K = 2
  
# initializing Repl char
repl_char = "*"
  
# Replace characters after K occurrences
# Using join() + count() + enumerate()
res = "".join(chr if test_str.count(chr, 0, idx) < K
      else repl_char for idx, chr in enumerate(test_str))
  
# printing result 
print("The string after performing replace : " + res) 

chevron_right


Output :

The original string is : geeksforgeeks is best for geeks
The string after performing replace : geeksforg**ks i* b**t*for******



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.