The trick is to provide a function as an argument to repl for re.sub() :
In [7]: re.sub(r'(\d+)', lambda m:'%.0f'%(float(m.group(1))*2), 'test line 123') Out[7]: 'test line 246'
Each match is converted to float , doubled, and then converted to string using the appropriate format.
This might be a little simplified if the number is an integer, but your question specifically mentions a float , so I used.
source share