from lxml import etree
data = """<everything>
<m>Some text before <r/></m>
<m><r/> and some text after.</m>
<m><r/></m>
<m>Text before <r/> and after</m>
<m><b/> Text after a sibling <r/> Text before a sibling<b/></m>
</everything>
"""
f = etree.fromstring(data)
for r in f.xpath('//r'):
    r.tail = 'DELETED' + r.tail if r.tail else 'DELETED'
etree.strip_elements(f, 'r', with_tail=False)
print(etree.tostring(f, pretty_print=True))
How can one replace an element with text in lxml?