What would be the python equivalent of the following perl script? I use the idiom often for quick templating tasks. It finds every occurrence of %%WHATEVER%% in $source and replaces it with the results of a subroutine call to sub whatever (lowercased) and is passed $variable. $source=~ s/%%(.*?)%%/lc($1)->($variable)/eg ; Thanks in advance.