better way than: myPage += 'more html' , ...

Alan Kennedy alanmk at hotmail.com
Wed Jun 25 18:36:29 CEST 2003


Gobo Borz wrote:

> I remember reading somewhere that building the string with successive
> "+=" statements is inefficient, but I don't know any alternatives, and
> my search attempts came up empty. Is there a better, more efficient way
> to do it than this:
> 
> #---------------------------------
> htmlPage = "<html><header></header><body>"
> htmlPage += "more html"
> ...
> htmlPage += "even more html"
> ...
> htmlPage += "</body></html>"
> print htmlPage
> #-------------------------------------

Best to do something like

#---------------------------------

htmlbits = []
htmlbits.append("<html><header></header><body>")
htmlbits.append("more html")
# ...
htmlbits.append("even more html")
# ...
htmlbits.append("</body></html>")

# And now for the non-intuitive bit
print "".join(htmlbits)

#---------------------------------

HTH,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list