How to Remove a Substring in Python? Last Updated : 20 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 to achieve this.The simplest and most common method to remove a substring from a string is by using the replace() method. This method returns a new string where all occurrences of the specified substring are replaced (in this case, with an empty string). Python # Using replace() method s = "Hello, world! Hello, Python!" sub = s.replace("Hello, ", "") print(sub) Outputworld! Python! replace() method replaces all occurrences of the substring "Hello, " with an empty string, effectively removing it from the original string.Let's take a look at other methods of removing a substring in python:Table of ContentUsing String SlicingUsing Regular ExpressionsUsing String Slicing - to remove specific substring by indexIf you need to remove a specific substring by its index, string slicing can be used. This approach is useful when you know the exact position of the substring to remove. Python # Using slicing to remove substring by index s = "Python is fun" index = s.find("is ") if index != -1: index = s[:index] + s[index + len("is "):] print(index) OutputPython fun This code uses slicing to remove the substring "is " from the string "Python is fun". It finds the starting index of the substring and then constructs a new string by excluding the specified range.Using Regular ExpressionsFor more complex removal scenarios, you can use regular expressions with the re module. This allows for pattern-based removal of substrings. Python import re # Using regular expressions to remove a substring s = "Hello, world! Hello, Python!" sub = re.sub(r"Hello, ", "", s) print(sub) Outputworld! Python! re.sub() method replaces all matches of the specified pattern with an empty string, effectively removing it from the original string. Comment More infoAdvertise with us Next Article Remove URLs from string in Python A anuragtriarna Follow Improve Article Tags : Python Python Programs python-string Python string-programs Practice Tags : python Similar Reads 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 - Remove after substring in String 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 includin 3 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 How to Get Index of a Substring in Python? To get index of a substring within a Python string can be done using several methods such as str.find(), str.index(), and even regular expressions. Each approach has its own use case depending on the requirements. Letâs explore how to efficiently get the index of a substring.The simplest way to get 2 min read How to Find Index of a Substring in Python In Python, sometimes we need to know where a certain substring appears in a string. To do this, we can find the index (or position) of that substring. The index tells us where the substring is located. Letâs look at some simple ways to find the index of a substring in a string.Using find() methodThe 2 min read 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 Like