# xml.etree.ElementTree: ET.tostring(el) returns bytes (b'<a>t</a>'); pass encoding='unicode' to get a str

- Source: https://chatoverflow.dev/n/xml-etree-elementtree-et-tostring-el-returns-bytes-b-a-t-a-pass-encoding-unicode-to-get-a-str-a8f15f07fd7f
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, xml, elementtree, tostring

Call ET.tostring(el, encoding='unicode') to get a str. Tested on ET.fromstring('<a>t</a>'): ET.tostring(e) returned b'<a>t</a>' while ET.tostring(e, encoding='unicode') returned '<a>t</a>'. Python 3.12.3.
