Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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

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
Python字符串替换Dict:跳过键并离开格式化程序_Python_String_String Formatting_String Substitution - Fatal编程技术网

Python字符串替换Dict:跳过键并离开格式化程序

Python字符串替换Dict:跳过键并离开格式化程序,python,string,string-formatting,string-substitution,Python,String,String Formatting,String Substitution,我是一个字符串代替品,需要几个VAULE,我想知道是否可以跳过一个键并将其留在那里,而不填充空格 s='%(name)s has a %(animal)s that is %(animal_age)s years old' #skip the animal value s = s % {'name': 'Dolly', 'animal': 'bird'}#, 'animal_age': 10} print s 预期产出: 您可以在字符串中使用两个%%,以跳过字符串格式设置: In [169

我是一个字符串代替品,需要几个VAULE,我想知道是否可以跳过一个键并将其留在那里,而不填充空格

s='%(name)s has a %(animal)s that is %(animal_age)s years old'

#skip the animal value
s = s % {'name': 'Dolly', 'animal': 'bird'}#, 'animal_age': 10}

print s
预期产出:
您可以在字符串中使用两个
%%
,以跳过字符串格式设置:

In [169]: s='%(name)s has a %(animal)s that is %%(animal_age)s years old'

In [170]: s % {'name': 'Dolly', 'animal': 'bird', 'animal_age': 10}

Out[170]: 'Dolly has a bird that is %(animal_age)s years old'
或者使用
string.format()


你的意思是跳过这里的
animal\u age
值吗?没错,有没有办法?
In [169]: s='%(name)s has a %(animal)s that is %%(animal_age)s years old'

In [170]: s % {'name': 'Dolly', 'animal': 'bird', 'animal_age': 10}

Out[170]: 'Dolly has a bird that is %(animal_age)s years old'
In [172]: s='{name} has a {animal} that is %(animal_age)s years old'

In [173]: dic = {'animal': 'bird', 'animal_age': 10, 'name': 'Dolly'}

In [174]: s.format(**dic)
Out[174]: 'Dolly has a bird that is %(animal_age)s years old'