How to use re.sub() in Python

The re module provides functions and support for regular expressions. re.sub() is used to replace substrings in strings.

Solution for How to use re.sub() in Python : You can use re.sub() to replace patterns in strings Call re.sub(pattern, repl, string) to return the string obtained by replacing the leftmost non-overlapping occurrences of the regex pattern in string with repl.

Call re.sub(pattern, repl, string, count=None) and include the optional count parameter to specify the maximum number of pattern occurrences to be replaced by repl in string.

a_string = "abc xxx abc xxx"