Python - Phrase removal in String Last Updated : 16 Mar, 2023 Summarize Comments Improve Suggest changes Share 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 - Remove Punctuation from String M 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 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 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 Like