How can I display text in the center of the terminal screen using shutil.get_terminal_size().columns and shutil.get_terminal_size().lines ?

enter image description here

  • one
    output to the line and add spaces to the beginning of the line (their number will be (terminal width - line width) / 2)? - pavel
  • @pavel I corrected a little. Look at the image. - CockLobster
  • Well, then I can offer the same thing, but take the indent to take the minimum of all the lines to output. - pavel

2 answers 2

If the text is already broken into lines, then it is enough just to format:

 >>> import shutil >>> lines = ['String right here', 'And here', 'Here', 'Aaa-and here'] >>> width = shutil.get_terminal_size().columns >>> position = (width - max(map(len, lines))) // 2 >>> for line in lines: # left justtified ... print(' '*position + line) ... String right here And here Here Aaa-and here >>> for line in lines: # right justified ... print(line.rjust(width // 2)) ... String right here And here Here Aaa-and here >>> for line in lines: # center ... print(line.center(width)) ... String right here And here Here Aaa-and here 
  • And how can you center the text (third example) with alignment to the left? - CockLobster
  • The first example aligns left - jfs
  • No, that it was exactly in the center of the screen, as in the image. - CockLobster
  • What is your difference from the image in question? Where to move the text: left, right, top, bottom? - jfs
  • did so: nl = (height - max(map(len, textArray))) // 2 + 11;print('\n'*nl) Everything works. - CockLobster

If you want to print in an arbitrary place of the screen in the terminal, then you can use the blessings module :

 #!/usr/bin/env python from blessings import Terminal # $ pip install blessings lines = ['String right here', 'And here', 'Here', 'Aaa-and here'] term = Terminal() with term.hidden_cursor(), term.fullscreen(): for i, line in enumerate(lines): x = (term.width - max(map(len, lines))) // 2 y = (term.height - len(lines)) // 2 + i with term.location(x, y): print(term.bold_white_on_black(line)) with term.location(0, term.height - 1): input('press <Enter> to exit..') 

The terminal looks like this:










                                String right here
                                And here
                                Here
                                Aaa-here









 press <Enter> to exit ..

In this case, the contents of the terminal for the duration of the program is cleared — when you exit the program, the terminal is restored.

Together with the colorama module , some features may also work on Windows.


For fun, you can try the asciimatics module :

 #!/usr/bin/env python from asciimatics.effects import Print # $ pip install asciimatics from asciimatics.renderers import FigletText, SpeechBubble, Rainbow from asciimatics.scene import Scene from asciimatics.screen import Screen from asciimatics.exceptions import ResizeScreenError def demo(screen): lines = ['String right here', 'And here', 'Here', 'Aaa-and here'] renderers = [Rainbow(screen, FigletText(line, font='small')) for line in lines] x = (screen.width - max(r.max_width for r in renderers)) // 2 H = max(r.max_height for r in renderers) - 1 # text height effects = [Print(screen, renderer, y=(screen.height - H * len(renderers)) // 2 + i * H, x=x) for i, renderer in enumerate(renderers)] effects.append(Print(screen, SpeechBubble("Press X to exit"), screen.height - 5, speed=1, transparent=False, start_frame=100)) screen.play([Scene(effects, -1)], stop_on_resize=True) if __name__ == "__main__": while True: try: Screen.wrapper(demo) except ResizeScreenError: continue else: break 

print-asciimatics tty session

Other available effects can be viewed in the samples folder .