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'
最佳答案
您应该将 {
替换为 {{
/}
替换为 }}
来表示 {
/}
字面意思。
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;
}
关于python - 格式化多行字符串时换行符出现 keyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43971564/