Python regex `sub` function for search and replace

Python regex substitute function.

Python search and replace #

The sub() function replaces every occurrence of a pattern with a string or the result of a function.

Syntax #

re.sub(pattern, repl, string, maximum=0)

This method replaces all occurrences of the re pattern in string with repl, substituting all occurrences unless a maximum value is provided. Finally, returns the modified string.

Get hands-on with 1200+ tech skills courses.