Sunday, 18 August 2013

Python: Can we Loop inside a string?

Python: Can we Loop inside a string?

Like for example I have a string of HTML Ordered List. Inside that ordered
list, I want to write n number of lists. How can I accomplish the task of
adding the lists to this string?
Here is the example code:
html = """
<ol>
<li>
<!--Something-->
</li>
... <!--n lists-->
{} #str().format()
<li>
<!--Something-->
</li>
</ol>
"""
for li in html_lists: #where li is <li>...</li> and is inside the
python list.
html.format(li)
As far as I know, Strings are immutable and .format() will add <li> at {}.
Hence this won't work for more than one <li>.

No comments:

Post a Comment