Code which replaces sys.stdout is disruptive and StringIO is typically too verbose for concatenation unless it’s being passed to or from code which already operates on files.
Using += should be fine for small run-once scripts and smaller strings. For larger templates one should append the strings to a list and then join them at the end:
outHTML: list[str] = []
outHTML.append("<HTML line 1>\n")
outHTML.append("<HTML line 2>\n")
outHTML.append("<HTML line 3>\n")
print("outHTML:", "".join(outHTML))
This is the second post that you are posting on how to generate HTML page using Python.
If we know exactly what you want, we might be better able to help you.
For example, do you need an HTML page with - JavaScript in it?