While looking for a solution… I think there might be something around .tail. I don't really understand the .tail principle and an answer could be there.
No?
OK. This works.
No?
OK. This works.
import xml.etree.ElementTree as ET import re action = ET.Element('act') action.text = 'A guy enters the room and **Slam!** shuts the door.' look = re.search('\*\*.+\*\*',action.text) if look != None: t1 = action.text[0:look.span()[0]] t2 = action.text[look.span()[0]+2:look.span()[1]-2] t3 = action.text[look.span()[1]:] action.text = t1 bold = ET.SubElement(action,'bold') bold.text = t2 bold.tail = t3 print(ET.tostring(action))If someone finds something to do better…