Python – Replace all occurrences of a substring in a string
Sometimes, while working with Python strings, we can have a problem in which we need to replace all occurrences of a substring with other.
Input : test_str = “geeksforgeeks” s1 = “geeks” s2 = “abcd”
Output : test_str = “abcdsforabcds” Explanation : We replace all occurrences of s1 with s2 in test_str.
Input : test_str = “geeksforgeeks” s1 = “for” s2 = “abcd”
Output : test_str = “geeksabcdgeeks”
We can use inbuilt function replace present in python3 to replace all occurences of substring.
Implementation using the inbuilt function:-