Skip to main content
2 of 3
added 16 characters in body
John Smith Optional
  • 25.4k
  • 14
  • 47
  • 68

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()) 
John Smith Optional
  • 25.4k
  • 14
  • 47
  • 68