0

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
aless80
  • 3,122
  • 3
  • 34
  • 53
  • Does this answer your question? [Python - Print at a given position from the left of the screen](https://stackoverflow.com/questions/54860229/python-print-at-a-given-position-from-the-left-of-the-screen) – clubby789 Jan 16 '20 at 14:16
  • I am trying with the following but it does not help: print(f'{command}{"":<100}{help}') and print(f"{'help':>100}\r{command:<30}") . In particular, the second would be fine but it prints the help string after the first line, not the last – aless80 Jan 16 '20 at 14:54

0 Answers0