简体   繁体   中英

How to pass the whole list in python string format?

Is there any way to pass the whole list in python string format

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)

Instead of passing individual list values, I want to pass the whole list print t.format(['John','10','A']) but this return IndexError: tuple index out of range . Is there any way to do this?

Just unpack the list:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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