SKTLZ
SKTLZ

Reputation: 183

Python/Format - List and width

This is how my format is working so far for my list :

print('{:>15} {:>15} {:>15}{:>15}'.format(*i_liste_l))

I'm very new to python and I can`t figure out how to set a dynamic width by dynamic I mean

len_total = int(len(str(mi_f)))
if len_total >= 15:
    width = int(len_total)
else: width = int(15)
print('{:>width} {:>width} {:>width}{:>width}'.format(*i_liste_l))

Is this possible ? and do I have to set the width for all my item in the list ?

Upvotes: 1

Views: 671

Answers (1)

falsetru
falsetru

Reputation: 369064

You can specify width as follow (passing keyword argument width):

>>> '{:>{width}} {:>{width}}'.format(1, 2, width=5)
'    1     2'

Upvotes: 4

Related Questions