What is the most pythonic way to build up large strings?
Asaf Las
roegltd at gmail.com
Sat Feb 8 03:13:54 EST 2014
On Saturday, February 8, 2014 9:41:53 AM UTC+2, cstru... at gmail.com wrote:
> I am writing a couple of class methods to build up several
> lines of html. Some of the lines are conditional and most need
> variables inserted in them. Searching the web has given me a
> few ideas. Each has its pro's and cons.
>
> The best I have come up with is:
>
> def output_header_js(self, jquery=True, theme=None):
> if self.static_path is None :
> return None
>
> if jquery is True:
> output = '"<script type="text/javascript" '
> output += 'src="/%s/jquery/jqueryui.js"></script>'% static
> output += '"<script type="text/javascript" '
> output += 'src="/%s/jquery/jquery.js"></script>'% static
>
> if theme is not None:
> output += '<link href="/%s/jtable/themes/%s/jtable.css" '% static, theme
>
> output += 'rel="stylesheet" type="text/css" />'
>
> output += '"<script type="text/javascript" '
> output += 'src="/%s/jtable/jquery.jtable.js"></script>' % "static"
>
> I realize that a lot of the above looks repetitive but it is
> designed to eliminate boilerplate HTML.
>
note, due to strings are immutable - for every line in sum operation
above you produce new object and throw out older one. you can write
one string spanned at multiple lines in very clear form.
/Asaf
More information about the Python-list
mailing list