Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将string.format与“一起使用”;{";已在字符串python中_Python_String - Fatal编程技术网

将string.format与“一起使用”;{";已在字符串python中

将string.format与“一起使用”;{";已在字符串python中,python,string,Python,String,字符串中已经有'{'。现在我想使用python格式方法 a = "{foo{}}" b = a.format("bar") 结果应该是{foobar} 有很多方法可以解决这个问题,但我想知道有没有一种方法可以先跳过{'要有一个常规大括号,请在格式字符串中使用它两次 >>> "{{foo{}}}".format("bar") '{foobar}' 您也可以使用%x语法: a = "{foo%s}" b = a % 'bar' 它将返回'{foobar}'很好 仅供参考:在P

字符串中已经有
'{'
。现在我想使用python格式方法

a = "{foo{}}"
b = a.format("bar")
结果应该是
{foobar}


有很多方法可以解决这个问题,但我想知道有没有一种方法可以先跳过
{'
要有一个常规大括号,请在格式字符串中使用它两次

>>> "{{foo{}}}".format("bar")
'{foobar}'

您也可以使用%x语法:

a = "{foo%s}"
b = a % 'bar'
它将返回
'{foobar}'
很好


仅供参考:在Python=>3.6中,要在字符串中打印%,可以使用
%%

,也可以使用f字符串,就像在
格式中一样,可以使用双大括号。