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

问题描述 投票:0回答:1

我有一个变量定义如下:

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,但这也不起作用。

任何帮助?

python html api
1个回答
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>
]]>""" 
© www.soinside.com 2019 - 2024. All rights reserved.