 <p>How about this?</p> <pre><code>&gt;&gt;&gt; re.sub('(\d+)', 'sub\g&lt;1&gt;', "C7H19N3") 'Csub7Hsub19Nsub3' </code></pre> <p><code>(\d+)</code> is a <a href="http://www.regular-expressions.info/brackets.html" rel="nofollow">capturing group</a> that matches 1 or more digits. <code>\g&lt;1&gt;</code> is a way of referring to the saved group in the substitute string.</p>