I would use the yattag library. I think it's the most pythonic way:
from yattag import Doc doc, tag, text = Doc().tagtext() with tag('food'): with tag('name'): text('French Breakfast') with tag('price', currency='USD'): text('6.95') with tag('ingredients'): for ingredient in ('baguettes', 'jam', 'butter', 'croissants'): with tag('ingredient'): text(ingredient) print(doc.getvalue()) FYI I'm the author of the library.