Python - Replace all occurrences of a substring in a string Last Updated : 08 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 occurrences of a substring in Python. Python a = "python java python html python" res = a.replace("python", "c++") print(res) Outputc++ java c++ html c++ Explanation:replace() method replaces all occurrences of the specified substring with the new string.This method is highly efficient for string replacement tasks and works seamlessly for all string sizes.Let's explore different methods to replace all occurrences of a substring in a string.Table of ContentUsing regular expressionsUsing string splitting and joiningUsing a manual loopUsing regular expressionsFor complex patterns, regular expressions allow us to replace substrings based on a pattern. Python import re s = "python java python html python" res = re.sub("python", "c++", s) print(res) Outputc++ java c++ html c++ Explanation:Regular expressions search for a specific pattern and replace all its matches with the replacement string.They are versatile and powerful for handling dynamic or complex patterns.Using string splitting and joiningThis approach involves splitting the string at the substring and rejoining the parts with the desired replacement. Python s = "python java python html python" res = "c++".join(s.split("python")) print(res) Outputc++ java c++ html c++ Explanation:split() method breaks the string into parts using the target substring.join() method combines these parts with the replacement string.While functional, this method is less efficient than replace for simple replacements.Using a manual loopA manual loop can iterate through the string and build a new one by replacing the target substring. Python s = "python java python html python" target = "python" replacement = "c++" res = "" i = 0 while i < len(s): if s[i:i+len(target)] == target: res += replacement i += len(target) else: res += s[i] i += 1 print(res) Outputc++ java c++ html c++ Explanation:This method manually searches for the target substring and replaces it with the desired string. Comment More infoAdvertise with us Next Article Python - Replace all occurrences of a substring in a string manjeet_04 Follow Improve Article Tags : Python Python string-programs Practice Tags : python Similar Reads replace() in Python to replace a substring 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:Pythons = "hlo AB world" # Replace "AB" with "C" in `s 1 min read Replacing Characters in a String Using Dictionary in Python In Python, we can replace characters in a string dynamically based on a dictionary. Each key in the dictionary represents the character to be replaced, and its value specifies the replacement. For example, given the string "hello world" and a dictionary {'h': 'H', 'o': 'O'}, the output would be "Hel 2 min read Python - Replacing Nth occurrence of multiple characters in a String with the given character Replacing the Nth occurrence of multiple characters in a string with a given character involves identifying and counting specific character occurrences.Using a Loop and find()Using a loop and find() method allows us to search for the first occurrence of a substring within each list element. This app 2 min read Python - Check if substring present in string The task is to check if a specific substring is present within a larger string. Python offers several methods to perform this check, from simple string methods to more advanced techniques. In this article, we'll explore these different methods to efficiently perform this check.Using in operatorThis 2 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 Find all the patterns of â1(0+)1â in a given string using Python Regex A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0âs. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. 2 min read Python - Remove empty strings from list of strings When working with lists of strings in Python, you may encounter empty strings (" ") that need to be removed. We'll explore various methods to Remove empty strings from a list. Using List ComprehensionList comprehension is the most concise and efficient method to filter out empty strings. This method 2 min read Print all subsequences of a string in Python Given a string s of size n (1 ⤠n ⤠20), the task is to print all subsequences of string. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.Examples: Input: s = "abc"Output: ["", "a", "b", "c", " 3 min read Python | Remove Redundant Substrings from Strings List Given list of Strings, task is to remove all the strings, which are substrings of other Strings. Input : test_list = ["Gfg", "Gfg is best", "Geeks", "for", "Gfg is for Geeks"] Output : ['Gfg is best', 'Gfg is for Geeks'] Explanation : "Gfg", "for" and "Geeks" are present as substrings in other strin 5 min read Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx 8 min read Like