python 3.x - Insert words into string starting from specific index -


i have string looks : 'foo dooo kupa trooo bar'.

i know start , end point of word kupa , need wrap : <span> </span>.

after operation want string like : foo dooo <span>kupa</span> trooo bar

i cannot find built-in methods can nice.

basically there 2 options:

import re strg = 'foo dooo kupa trooo bar'  match = re.search('kupa', strg) print('{}<span>{}</span>{}'.format(strg[:match.start()], strg[match.start():match.end()], strg[match.end():])) 

or (the first expressioin regex, if pattern little more complicated):

print(re.sub('kupa', '<span>kupa</span>', strg)) 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -