You can apply formatting (with format()
or str.format()
) inside a list comprehension before joining:
''.join([format(el, '<10') for el in list_of_strings])
Demo:
>>> list_of_strings = ['foo', 'barbaz', 'spam-ham']
>>> ''.join([format(el, '<10') for el in list_of_strings])
'foo barbaz spam-ham '
<
left-aligns, >
right-aligns and ^
centers the text in the given width (10 in the above example). For more detail on how formatting works, see the format specification documentation.
Demo with right-adjustment to 4 spaces:
>>> list_of_strings = ['foo', 'ba', 'a']
>>> ''.join([format(el, '>4') for el in list_of_strings])
' foo ba a'