Python - Remove after substring in String Last Updated : 17 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring including the part of substring suppose the given substring is "sample" so we need to remove the part from the substring from the string so the output string will become "Hello, this is a". This can be done using methods like find(), split() or partition()Using split()We can use the split() method to split the string at the desired substring and keep only the part before it. Python s = "Hello, this is a sample string." substring = "sample" # Split the string at the substring and take the first part result = s.split(substring)[0] print(result) OutputHello, this is a Explanation:Split(substring) method splits the string s into a list at each occurrence of the substring "sample".Indexing with [0], the code retrieves the part of the string before the substring, resulting in "Hello, this is a "Using find() and String SlicingUsing find() to locate the position of the substring and then slice the string to get everything before that position. Python s = "Hello, this is a sample string." substring = "sample" # Find the position of the substring and slice the string pos = s.find(substring) if pos != -1: result = s[:pos] # Everything before the substring else: result = s # If the substring is not found, keep the original string print(result) OutputHello, this is a Explanation:Find(substring) method locates the starting position of the substring "sample" within the string s. If found, it returns the index; otherwise, it returns -1.Substring is found, the string is sliced up to the position of the substring, extracting everything before it. If not found, the original string is kept intact.Using partition()partition() method divides the string into three parts: the part before the substring, the substring itself, and the part after. We can discard the part after the substring. Python s = "Hello, this is a sample string." substring = "sample" # Partition the string at the substring and keep only the first part result = s.partition(substring)[0] print(result) # Output: Hello, this is a OutputHello, this is a Explanation:Partition(substring) method splits the string into a tuple containing three parts: the part before the substring, the substring itself, and the part after.Indexing with [0], the code retrieves the portion of the string before the substring, resulting in "Hello, this is a ". Comment More infoAdvertise with us Next Article Python - Remove after substring in String manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by 3 min read 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 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 the given substring from end of string Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Â Â Remove the substring from the end of the string using Slicing In 3 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 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 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 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 - Get all substrings of given string A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element 3 min read Python | Removing Initial word from string During programming, sometimes, we can have such a problem in which it is required that the first word from the string has to be removed. These kinds of problems are common and one should be aware about the solution to such problems. Let's discuss certain ways in which this problem can be solved. Met 4 min read Like