print("xy{0}z".format(100))
xy100z
让我们尝试多行的字符串 字符串我想格式化它。
strs='''
.item{0}{
background-image:url("img/item{0}_1.jpg");
background-repeat: no-repeat;
background-position: 0px 0px;
}'''
>>> print(strs)
.item{0}{
background-image:url("img/item{0}_1.jpg");
background-repeat: no-repeat;
background-position: 0px 0px;
}
现在用一些数字格式化它。
print(strs.format(100))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: '\n background-image'
答案 0 :(得分:2)
您应该将{
替换为{{
/ }
,}}
代替{
/ }
。{/ p>
strs='''
.item{0}{{
background-image:url("img/item{0}_1.jpg");
background-repeat: no-repeat;
background-position: 0px 0px;
}}'''
print(strs.format(100))
打印:
.item100{
background-image:url("img/item100_1.jpg");
background-repeat: no-repeat;
background-position: 0px 0px;
}