Python RegEx sub() Functions

The re.sub() function is used to replace occurrences of a particular sub-string with another sub-string.


import re
res=re.sub(pattern, repl, string, count=0, flags=0)

The ‘sub’ in the function stands for SubString, a certain regular expression pattern is searched in the given string(3rd parameter), and upon finding the substring pattern is replaced by repl(2nd parameter), count checks and maintains the number of times this occurs. 


import re
str = "Welcome to Python Programming"
res = re.sub("\s", " !! ", str, 1)

Output: Welcome !! to Python Programming