2

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(" ")

2 Answers2

3

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.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
1

You can use the format alignment options but you still have to set the string's total length though.

'{:>10}'.format('Hello')
  • '>' means align right.
  • '10' is the character length you deisre.

More format options here

In case you really need the terminal size you can extract like that.