Making letters uppercase using re.sub in python?

You can pass a function to re.sub() that will allow you to do this, here is an example:

 def upper_repl(match):
     return 'GOO' + match.group(1).upper() + 'GAR'

And an example of using it:

 >>> re.sub(r'foo([a-z]+)bar', upper_repl, 'foobazbar')
 'GOOBAZGAR'

Leave a Comment