繁体   English   中英

如何将列表的值放入字符串

[英]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, 2]
print 'ID {0}, {1}, and {2}.'.format(*ID)

请参阅文档: 解压缩参数列表

>>> 'ID {0}, {1}, and {2}.'.format(*ID)
'ID 0, 1, and 2.'

您需要打开列表包装。

您的第二个代码没有多大意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM