python 一行,Python:如何在多行字符串中插入一行?

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 too long). I want to write a function that will throw a error under a certain condition, but I don't know how to insert a line in the string "page".

page="""

Sign up

.label {text-align: right}

.error {color: red}

Sign up

Username

Password

I want to insert the line "There is an error" in the blank between these two lines:

Note there are two of this group in the string. How can I insert "There is an error" in the first group using Python? Thanks.

解决方案

python uses C style format strings.

page="""

......

%s

""" % "There was an error"

alternatively, you can use python string.format as

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

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(""" ....

{{x}}

""")

page.render(x="There was an error")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值