繁体   English   中英

如何以python字符串格式传递整个列表?

[英]How to pass the whole list in python string format?

有没有办法以python字符串格式传递整个列表

i=['John','10','A']

t="{:<10}| {:<10}| {:<10}"
print t.format("Name","Age","Grade")
# print t.format(i[0],i[1],i[2])
print t.format(i)

而不是传递单个列表值,我想传递整个列表print t.format(['John','10','A'])但这会返回IndexError: tuple index out of range 有没有办法做到这一点?

只需unpack列表:

>>> i=['John','10','A']
>>> t="{:<10}| {:<10}| {:<10}"
>>> t.format(*i)
'John      | 10        | A         '

暂无
暂无

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

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