replace() in Python to replace a substring Last Updated : 31 Jan, 2025 Comments Improve Suggest changes Like Article Like Report replace() method in Python allows us to replace a specific part of a string with a new value. It returns a new string with the change without modifying the original one. We can also specify how many times the replacement should occur.For Example: Python s = "hlo AB world" # Replace "AB" with "C" in `s` res = s.replace("AB", "C") print(res) Outputhlo C world Table of ContentSyntax of replace()Removing extra spaces by using replace()Converting a date formatSyntax:str.replace(pattern,replaceWith,maxCount)Parameters:pattern: This specify string that we want to search for.replaceWith: This specify string we want to replace the old value with.maxCount: This specify how many times we want to replace something. If we don't set it then all occurrences will be replaced.Removing extra spaces by using replace()It means replacing multiple spaces between words with a single space or completely removing them to clean up the text. Python s = "Geeks For Geeks" # Replacing all spaces with an empty string res = s.replace(" ", "") print(res) OutputGeeksForGeeks Converting a date formatWe can use replace() to convert a date format from one style to another. Python # Backend date format date = "2025-01-21" # Converting the date format to DD/MM/YYYY res = date.replace("-", "/").split("/") res = f"{res[2]}/{res[1]}/{res[0]}" print(res) Output21/01/2025 Comment More infoAdvertise with us Next Article replace() in Python to replace a substring S Shashank Mishra Follow Improve Article Tags : Strings Python DSA python-string Practice Tags : pythonStrings Similar Reads Python | Pandas Series.str.replace() to replace text in a series Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Pandas Series.str.replace() method works like Python .replace() method only, but it works o 5 min read How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str 4 min read Python String replace() Method The replace() method replaces all occurrences of a specified substring in a string and returns a new string without modifying the original string.Letâs look at a simple example of replace() method.Pythons = "Hello World! Hello Python!" # Replace "Hello" with "Hi" s1 = s.replace("Hello", "Hi") print( 2 min read Python - Replace all occurrences of a substring in a string Replacing all occurrences of a substring in a string means identifying every instance of a specific sequence of characters within a string and substituting it with another sequence of characters. Using replace()replace () method is the most straightforward and efficient way to replace all occurrence 2 min read How to search and replace text in a file in Python ? In this article, we will learn how we can replace text in a file using python. Method 1: Searching and replacing text without using any external module Let see how we can search and replace text in a text file. First, we create a text file in which we want to search and replace text. Let this file b 5 min read Python | Pandas Series.replace() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.replace() function is used to 3 min read re.subn() in Python re.subn() method in Python is used to search for a pattern in a string and replace it with a new substring. It not only performs the replacement but also tells us how many times the replacement was made. We can use this method when we need to replace patterns or regular expressions in text and get a 3 min read How to Remove Letters From a String in Python Removing letters or specific characters from a string in Python can be done in several ways. But Python strings are immutable, so removal operations cannot be performed in-place. Instead, they require creating a new string, which uses additional memory. Letâs start with a simple method to remove a s 3 min read Python String - removeprefix() function Python String removeprefix() function removes the prefix and returns the rest of the string. If the prefix string is not found, then it returns the original string.Example:Pythons1 = 'Geeks'.removeprefix("Gee") print(s1)Output:ksLet's take a deeper look at removeprefix():Table of ContentSyntax:Examp 2 min read Python | Pandas Timestamp.replace Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Pandas Timestamp.replace() function is used to replace the member values of the given 3 min read Like