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

Dave Kuhlman dkuhlman at rexx.com
Wed Jun 25 23:07:48 CEST 2003


Andy Jewell wrote:

[snip]

> An easy way, which requires little changes tou your code would be
> to use a list and join it up into a string in one go at the end:
> 
> -----------8<--------------
> htmlPage=["<html><header></header><body>\n"]
> htmlPage.append("more html\n")
> ....
> htmlPage.append("more html\n")
> ....
> htmlPage.append("<\body><\html>\n")
> htmlDoc="".join(htmlPage)
> -----------8<--------------

If you find that you are explicitly adding a new-line at the end
of every string that you append, as a convenience, you might want
to consider using the following minor modification:

-----------8<--------------
import string
...
htmlPage=["<html><header></header><body>"]
htmlPage.append("more html")
....
htmlPage.append("more html")
....
htmlPage.append("<\body><\html>")
# Use string.join() instead of ''.join() to add new-lines.
htmlDoc= string.join(htmlPage, '\n')
-----------8<--------------

Funny that string.join() supports the optional second parameter,
while 'abc'.join() does not.

  - Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list