The basic question is: I know how to right align, i.e. to apply a padding of 10 spaces:
'some string %10s' % 'place me 10 spaces after previous character'
but how to print text at an absolute horizontal position on a line?
I am formatting help pages, i.e. a series of string pairs using the Format Specification Mini-Language. As you can see below, I am using a template and .format to write lines consisting of a command and a help string. The first output line is fine, but then I split the command in multiple lines and for this reason the help string is not rendered at horizontal position 100 (as indicated in the template):
template = "{0:30} {1:100}\n"
command_help = [
('myscript --help',
'Show the help'),
('myscript do_this --option1=10 \n --option2=3 ...',
'Script does something. NB bad padding'),
('myscript do_this --option1=10 \n --option2=3 ...',
'%28s' % 'Manually adding padding does not work'),
('myscript do_first \nmyscript do_second \nmyscript do_third',
'Common command sequence to do something. NB bad padding')
]
for command, help in command_help:
print(template.format(command, help))
Result:
myscript --help Show the help
myscript do_this --option1=10
--option2=3 ... Script does something. NB bad padding
myscript do_this --option1=10
--option2=3 ... Manually adding padding does not work
myscript do_first
myscript do_second
myscript do_third Common command sequence to do something. NB bad padding