Given a string str that may contain one more occurrences of “AB”. Replace all occurrences of “AB” with “C” in str.
Input : str = "helloABworld" Output : str = "helloCworld" Input : str = "fghABsdfABysu" Output : str = "fghCsdfCysu"
This problem has existing solution please refer Replace all occurrences of string AB with C without using extra space link. We solve this problem in python quickly using replace() method of string data type.
How does replace() function works ?
str.replace(pattern,replaceWith,maxCount) takes minimum two parameters and replaces all occurrences of pattern with specified sub-string replaceWith. Third parameter maxCount is optional, if we do not pass this parameter then replace function will do it for all occurrences of pattern otherwise it will replace only maxCount times occurrences of pattern.
- Python | Replace substring in list of strings
- Python - Replace all occurrences of a substring in a string
- Python | Pandas Series.str.replace() to replace text in a series
- SequenceMatcher in Python for Longest Common Substring
- Python | Check if a Substring is Present in a Given String
- Python | Find longest consecutive letter and digit substring
- Python | Check if substring present in string
- Python | Finding strings with given substring in list
- Python | Check if substring is part of List of Strings
- Python | Duplicate substring removal from list
- Python | Find last occurrence of substring
- Python | Get the string after occurrence of given substring
- Python | Filter list of strings based on the substring list
- Python | Get the substring from given string using list slicing
- Python | Remove the given substring from end of string
- Python | Add substring at specific index
- Python | Get the starting index for all occurrences of given substring
- Python | Frequency of substring in given string
- Python | All occurrences of substring in string
- Python | Ways to find nth occurrence of substring in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vaibhav2992