0

I'm wanting a python function like this:

printSeperated("leftString", "rightString", screenWidth)

That would output:

leftString                    |                        rightString

Such that the "|" delimiter is at the midpoint of the screen.

I've tried:

def printSeperated(str1, str2, w = 20):
  print(str1.ljust(w), "|".center(w), str2.rjust(w))

But this does not keep the delimiter at the center of the screen

CraigDavid
  • 1,046
  • 1
  • 12
  • 26
  • Does this answer your question? [String formatting: Columns in line](https://stackoverflow.com/questions/19103052/string-formatting-columns-in-line) – Joe Mar 18 '22 at 03:32
  • The default sep option, space, makes the result. Please use string concatination, '+' , instead of ','. `print(str1.ljust(w) + "|".center(w) + str2.rjust(w))` – hochae Mar 18 '22 at 03:52
  • 1
    The question referenced by @Joe is helpful, but I continue to struggle if the left string is longer than the specified width... it seems there is no way to always force the delimiter to always be in the middle of the screen. – CraigDavid Mar 18 '22 at 04:01
  • As mentioned here, you can't operate with the *screen* width, all you can do is operate the number and position of symbols. So you ARE able to keep the delimiter in the exact position as per the number of symbols counting from the left of the buffer. But you can't tell the separator to stay exactly in the middle of the screen as the graphics output device. – s0mbre Mar 18 '22 at 05:09
  • Just adding a comment, that its probably better to use a table printing library instead of doing this manually – CraigDavid Mar 18 '22 at 19:00

1 Answers1

-1

print doesn't know anything about the screen/terminal width. You probably have to use something like the Curses library.

Visvamba Nathan
  • 135
  • 1
  • 6