Skip to content
Advertisement

Python – Formatting So When Text is Printed It Appears From the Right Side Not Left Side

The code below shows letter by letter of the sentence in parenthesis but it types from the left side when I run the program. Is there any way I can make the text print from the right side?

from time import sleep

hello = ("Hello")


for character in hello:
    print (character, end = "", flush = True)
    sleep(0.1)

print(" ")

Advertisement

Answer

You can use str.rjust() to right-justify a string to fit in a certain character length, padding the excess with a chosen character:

>>> "Hello".rjust(10, ' ')
#  '     Hello'

However, you can’t just right-justify everything you print in the terminal window, because you don’t know how large the terminal window is going to be. You can maybe assume the width will be 80 characters at minimum, but that’s not a hard gap.

You’ll probably get more mileage messing with your terminal’s display settings than you will writing a python program to automatically right-justify your text.

Advertisement