Sub

<re object>.sub (replacement,string[, count = 0])

Thesub ( ) method achieves the same result as the sub ( ) function described previously and has the same arguments (except pattern). Unlike the sub() function, however, which returns a list of strings, the sub() method returns the modified string if a match is found, or the original string argument if there are no matches.

>>> from re import * >>> from string import *

... return capitalize(match0bj.group(0))

cre = compile("monkey")

newStr = cre.sub(toUpper,"monkey see monkey do, ,,") print newStr The output is

Was this article helpful?

0 0

Post a comment