Skip to content Skip to sidebar Skip to footer

Python: How To Insert A Line In A Multi-line String?

I'm trying to make a simple website demo and I have some trouble editing the html using python. The following is the string I defined to store the html (not complete because it's t

Solution 1:

python uses C style format strings.

page="""<html>
       ......
      <td class="error">
         %s
      </td>
    </tr>"""  % "There was an error"

alternatively, you can use python string.format as

"fooo {x}".format(x="bar"). 

see (https://docs.python.org/2/library/string.html);

jinja is an excellent template engine if you've got the time. Genshi (https://genshi.edgewall.org/wiki/GenshiTutorial) is worth looking into as well.

for Jinja2:

pip install Jinja2

in python

from jinja2 import Template
page = Template("""<html> .... 
<td class="error">{{x}}</td>
""")
page.render(x="There was an error")

Post a Comment for "Python: How To Insert A Line In A Multi-line String?"