replace() in Python to replace a substring Last Updated : 31 Jan, 2025 Summarize Comments Improve Suggest changes Share 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 How to Search and Replace Text in a file in Python S Shashank Mishra Follow Improve Article Tags : Python python-string Practice Tags : python 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âll explore four effective methods to search and replace text within a file using Python. These methods range from basic built-in functions to powerful modules like re and fileinput.Method 1: Using Basic File Handling (open() and replace())Let see how we can search and replace tex 3 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 Like