【发布时间】:2015-11-12 20:46:37
【问题描述】:
我想填写一个具有特定格式的字符串。当我只有一个值时,构建它很容易:
>>> x = "there are {} {} on the table.".format('3', 'books')
>>> x
'there are 3 books on the table.'
但是如果我有一长串对象怎么办
items =[{'num':3, 'obj':'books'}, {'num':1, 'obj':'pen'},...]
我想以完全相同的方式构造句子:
There are 3 books and 1 pen and 2 cellphones and... on the table
鉴于我不知道列表的长度,我怎么能做到这一点?使用format 可以轻松构造字符串,但我必须事先知道列表的长度。
【问题讨论】:
标签: python string python-3.x