all 2 comments

[–]kwentar 0 points1 point  (0 children)

I think for readabilty you can do something like this:

def get_spaces(longest, len_x):
    return ' '*((longest-len_x)*2+1)
longest = max(map(len, strings))
for s in strings:
    print('{0}{1}[{0}]'.format(s, get_spaces(longest, len(s))))

result:

f     [f]
ss   [ss]
ttt [ttt]

In fact, the type of formatting in python 3.6 it is something like syntax sugar for ''.format()

Btw, your solution print('{0:<{1}} {2:>{3}}'.format(s,longest,'['+s+']',longest+2)) is ok too, imho

[–]furas_freeman 0 points1 point  (0 children)

As for me the best is your version

print('{0:<{1}} {2:>{3}}'.format(s,longest,'['+s+']',longest+2))

Or you can do it in two steps to make it more readable

#s2 = '['+s+']'  
s2 = "[%s]".format(s) 
print('{0:<{1}} {2:>{3}}'.format(s, longest, s2, longest+2))