Python - Remove N characters after K Last Updated : 24 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a String, remove N characters after K character. Input : test_str = 'ge@987eksfor@123geeks is best@212 for cs', N = 3, K = '@' Output : 'geeksforgeeks is best for cs' Explanation : All 3 required occurrences removed. Input : test_str = 'geeksfor@123geeks is best for cs', N = 3, K = '@' Output : 'geeksforgeeks is best for cs' Explanation : @123 is removed. Method #1 : Using re.sub() In this, we specify appropriate regex to capture the element and to remove next N occurrences from String. The sub() is used to perform replacement. Python3 # Python3 code to demonstrate working of # Remove N characters after K # Using re.sub() import re # initializing strings test_str = 'geeksfor@123geeks is best@212 for cs' # printing original string print("The original string is : " + str(test_str)) # initializing N N = 3 # initializing K K = '@' # using re.sub() to perform task res = re.sub(r'\@...', '', test_str) # printing result print("The String after removal : " + str(res)) OutputThe original string is : geeksfor@123geeks is best@212 for cs The String after removal : geeksforgeeks is best for cs Time Complexity: O(n)Auxiliary Space: O(n), where n is length of string. Method #2 : Using re.sub() + occurrence option This is similar to above, just using 4th argument of re.sub() to control the occurrence counts we wish to perform replace. Python3 # Python3 code to demonstrate working of # Remove N characters after K # Using re.sub() + occurrence option import re # initializing strings test_str = 'geeksfor@123geeks is best@212 for cs' # printing original string print("The original string is : " + str(test_str)) # initializing N N = 3 # initializing K K = '@' # using re.sub() to perform task # controlling occurrence using 4th arg. # removes just 1st occurrence res = re.sub(r'\@...', '', test_str, 1) # printing result print("The String after removal : " + str(res)) OutputThe original string is : geeksfor@123geeks is best@212 for cs The String after removal : geeksforgeeks is best@212 for cs The time and space complexity for all the methods are the same: Time Complexity: O(n) Space Complexity: O(n) Comment More infoAdvertise with us Next Article Python - Replace multiple characters at once M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python | Replace characters after K occurrences Sometimes, while working with Python strings, we can have a problem in which we need to perform replace of characters after certain repetitions of characters. This can have applications in many domains including day-day and competitive programming. Method #1: Using loop + string slicing This is brut 5 min read Python | Append K character N times Sometimes, we wish to manipulate a string in such a way in which we might need to add additional K at the end of string in case of filling the missing bits or any other specific requirement. The solution to this kind of problems is always handy and is good if one has knowledge of it. Letâs discuss c 4 min read Python - Remove characters till K element Sometimes, while working with Python, we can have problem in which we need to get all elements in list occurring after particular character in list. This kind of problem can have application in data domains and web development. Lets discuss certain ways in which this task can be performed. Method #1 5 min read Python - Reverse Shift characters by K Given a String, reverse shift each character according to its alphabetic position by K, including cyclic shift. Input : test_str = 'bccd', K = 1 Output : abbc Explanation : 1 alphabet before b is 'a' and so on. Input : test_str = 'bccd', K = 2 Output : zaab Explanation : 2 alphabets before b is 'z' 3 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 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 Like