The new(ish) string format method lets you do some fun stuff with nested keyword arguments. The simplest case:
>>> '{message: <16}'.format(message='Hi')
'Hi '
If you want to pass in 16
as a variable:
>>> '{message: <{width}}'.format(message='Hi', width=16)
'Hi '
If you want to pass in variables for the whole kit and kaboodle:
'{message:{fill}{align}{width}}'.format(
message='Hi',
fill=' ',
align='<',
width=16,
)
Which results in (you guessed it):
'Hi '
And for all these, you can use python 3.6+ f-strings:
message = 'Hi'
fill = ' '
align = '<'
width = 16
f'{message:{fill}{align}{width}}'
And of course the result:
'Hi '
"%-6s" % s
for left-aligned and"%6s" % s
for right-aligned. – Basj