Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 670
Eka Avatar asked Mar 04 '23 23:03

Eka


1 Answers

Just unpack the list:

>>> i=['John','10','A']
>>> t="{:<10}| {:<10}| {:<10}"
>>> t.format(*i)
'John      | 10        | A         '
like image 163
Netwave Avatar answered Mar 08 '23 02:03

Netwave