Allign left and right in python?

I saw a question about justifying β€œprinting,” but could I have text left and right on the same line for -help? In a terminal, it would look like this:

| | |Left Right| | | 
+4
source share
2 answers

I think you can use sys.stdout for this:

 import sys def stdout(message): sys.stdout.write(message) sys.stdout.write('\b' * len(message)) # \b: non-deleting backspace def demo(): stdout('Right'.rjust(50)) stdout('Left') sys.stdout.flush() print() demo() 

You can replace 50 with the exact width of the console, which you can get from fooobar.com/questions/34408 / ...

+4
source

Here is a pretty simple way:

 >>> left, right = 'Left', 'Right' >>> print '|{}{}{}|'.format(left, ' '*(50-len(left+right)), right) |Left Right| 

As a function:

 def lr_justify(left, right, width): return '{}{}{}'.format(left, ' '*(width-len(left+right)), right) >>> lr_justify('Left', '', 50) 'Left ' >>> lr_justify('', 'Right', 50) ' Right' >>> lr_justify('Left', 'Right', 50) 'Left Right' >>> lr_justify('', '', 50) ' ' 
+3
source

Source: https://habr.com/ru/post/1400647/


All Articles