我有一个变量定义如下:
item_title="Title"
Run Code Online (Sandbox Code Playgroud)
我想将此值放入以下字符串中:
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>
]]>"""
Run Code Online (Sandbox Code Playgroud)
我在这里的尝试失败了。我还尝试在最后三个“””之后使用 {} 和 .format,但这也不起作用。
有什么帮助吗?
小智 6
您不能使用格式,因为您在样式部分中使用了这些括号,但您可以通过加运算符( ){}轻松完成,例如:-+
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>
]]>"""
Run Code Online (Sandbox Code Playgroud)