Python - Phrase removal in String Last Updated : 16 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Sometimes, while working with Python strings, we can have a problem in which we need to extract certain words in a string excluding the initial and rear K words. This can have application in many domains including all those include data. Lets discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + enumerate() + list slicing The combination of above methods can be used to solve this problem. In this, we extract the indices of spaces and perform the slicing according to space indices. Python3 # Python3 code to demonstrate working of # Phrase extraction in String # Using list comprehension + enumerate() + list slicing # initializing string test_str = 'Geeksforgeeks is best for geeks and CS' # printing original string print("The original string is : " + str(test_str)) # initializing K K = 2 # Phrase extraction in String # Using list comprehension + enumerate() + list slicing temp = [idx for idx, ele in enumerate(test_str) if ele == ' '] res = test_str[temp[K - 1]: temp[-(K - 1)]].strip() # printing result print("String after phrase removal : " + str(res)) Output : The original string is : Geeksforgeeks is best for geeks and CS String after phrase removal : best for geeks and Method #2 : Using join() + split() The combination of above methods can be used to perform this task. In this, we split all words and join all the words except the initial and rear K. Python3 # Python3 code to demonstrate working of # Phrase extraction in String # Using join() + split() # initializing string test_str = 'Geeksforgeeks is best for geeks and CS' # printing original string print("The original string is : " + str(test_str)) # initializing K K = 2 # Phrase extraction in String # Using join() + split() res = ' '.join(test_str.split()[K:-(K - 1)]) # printing result print("String after phrase removal : " + str(res)) Output : The original string is : Geeksforgeeks is best for geeks and CS String after phrase removal : best for geeks and The Time and Space Complexity for all the methods are the same: Time Complexity: O(n) Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Python - Phrase removal in String manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python | Substring removal in String list While working with strings, one of the most used application is removing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the removing of a substring in list of string is performed. Letâs discuss certain ways in which 5 min read Python - Retain Numbers in String Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for 2 min read Python | Remove prefix strings from list Sometimes, while working with data, we can have a problem in which we need to filter the strings list in such a way that strings starting with a specific prefix are removed. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + remove() + startswith() The combinati 5 min read Remove URLs from string in Python A regular expression (regex) is a sequence of characters that defines a search pattern in text. To remove URLs from a string in Python, you can either use regular expressions (regex) or some external libraries like urllib.parse. The re-module in Python is used for working with regular expressions. I 3 min read Python - Remove Punctuation from String In this article, we will explore various methods to Remove Punctuations from a string.Using str.translate() with str.maketrans()str.translate() method combined with is str.maketrans() one of the fastest ways to remove punctuation from a string because it works directly with string translation tables 2 min read Remove spaces from a string in Python Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so:Using replace() methodTo remove all spaces from a s 2 min read Python - Add Phrase in middle of String Given a String, add a phrase in the middle of it. Input : test_str = 'geekforgeeks is for geeks', mid_str = "good" Output : geekforgeeks is good for geeks Explanation : Added just in middle, after 2 words. Input : test_str = 'geekforgeeks best', mid_str = "is" Output : geekforgeeks is best Explanati 7 min read How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways 2 min read Remove Special Characters from String in Python When working with text data in Python, it's common to encounter strings containing unwanted special characters such as punctuation, symbols or other non-alphanumeric elements. For example, given the input "Data!@Science#Rocks123", the desired output is "DataScienceRocks123". Let's explore different 2 min read Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing 3 min read Like