如何将列表中的值放入字符串中
我想把列表中的几个值放到一个字符串里。下面是我写的代码:
ID = [0, 1, 2]
print 'ID {0}, {1}, and {2}.'.format(ID)
或者
print (r'(ID\s*=\s*)(\S+)').format(ID)
这个方法不行。有没有人知道我哪里出错了?第二行的代码打印出了列表:
[0, 1, 2]
第一行是这样写的:
File "tset.py", line 39, in b
print 'ID {0}, {1}, and {2}.'.format(ID)
IndexError: tuple index out of range
谢谢
2 个回答
9
4
>>> 'ID {0}, {1}, and {2}.'.format(*ID)
'ID 0, 1, and 2.'
你需要把你的列表拆开。
你第二段代码看起来没什么意义。