[英]How to put values of a list into a string
我正在尝试将列表中的多个值放入字符串中。 我的代码如下:
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
谢谢
>>> 'ID {0}, {1}, and {2}.'.format(*ID)
'ID 0, 1, and 2.'
您需要打开列表包装。
您的第二个代码没有多大意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.