I'd look for something that expresses what we're doing as clearly as I could:
Your code dealt with leading spaces, but I don't see them in your sample output. If you want to right justify this thing:
you might pick a line length & pad each line to that length before you print it.
You could figure the line length ... this kind of thing would be close enough to play with and fix up ...
Any of that sound like fun?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi