簡體   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