使用三引号将变量值添加到字符串中

时间:2018-07-22 03:13:01

标签: python html api

我有一个定义如下的变量:

item_title="Title"

我想将此值放入以下字符串:

        html_desc="""
<![CDATA[
<head>
<style>
body {
    font-size: 25px;
    border-style: solid;
    border-width: 5px;
    border-color: green;
    border-radius: 10px;
    background-color: #FFFFC2;
    padding: 15px;
}

li {
    color: #F7270F;
    font-weight: bold;
}
</style>
</head>
<body>
<h1 align="center">Auctions for Week of 08-01-2018</h1>
<p>You are bidding on the following item:</p>
<ul><li>"{}".format(item_title)</li></ul>
<p>Condition is pack fresh unless otherwise indicated. Please review the pictures carefully and if you have any questions about something specific, ask.</p>
<p><b>Shipping:</b> Shipping will be calculated based on buyer location. No premiums are charged. Cards are mailed in an 8x5 bubble mailer and shipped First Class mail unless the price exceeds $50, at which point they will be shipped Priority at no additional cost to the buyer. If you win multiple auctions, please wait for an invoice to be sent.</p>
</body>
]]>"""

我在这里的尝试被打破了。我也尝试过在最后三个“”之后的末尾使用{}和.format,但这都不起作用。

有帮助吗?

1 个答案:

答案 0 :(得分:2)

您不能使用格式,因为您在样式部分使用了这些{}括号,但是您可以通过 Plus运算符+)轻松地做到这一点,例如:-

html_desc="""
<![CDATA[
<head>
<style>
body {
    font-size: 25px;
    border-style: solid;
    border-width: 5px;
    border-color: green;
    border-radius: 10px;
    background-color: #FFFFC2;
    padding: 15px;
}

li {
    color: #F7270F;
    font-weight: bold;
}
</style>
</head>
<body>
<h1 align="center">Auctions for Week of 08-01-2018</h1>
<p>You are bidding on the following item:</p>
<ul><li>""" + item_title + """</li></ul>
<p>Condition is pack fresh unless otherwise indicated. Please review the pictures 
carefully and if you have any questions about something specific, ask.</p>
<p><b>Shipping:</b> Shipping will be calculated based on buyer location. No premiums are charged. Cards are mailed in an 8x5 bubble mailer and shipped First Class mail unless the price exceeds $50, at which point they will be shipped Priority at no additional cost to the buyer. If you win multiple auctions, please wait for an invoice to be sent.</p>
</body>
]]>"""