Reputation: 39
A simplified version of my problem is as follows. The current code/output I have is problematic.
Code:
file.write("AAAA" + "\n")
file.write(num + " BBBBB") #num is a random number
Current output:
AAAAA
20 BBBBB
I would like to align the A's and B's so that it looks like this:
AAAAA
20 BBBBB
Specifically though, I need the output to work for any number. I.e, "2000BBBB" etc.
Making it work for the example I have given is easy enough, I could just add spacing. The problem here is writing code that will essentially predict where the second line's B's will be located.
I hope that makes sense, thank you.
Upvotes: 0
Views: 1032
Reputation: 977
This should work:
first = "AAAA" + "\n"
second = str(num) + " BBBBB"
file.write(first.rjust(len(second)))
file.write(second)
Check those: Python docs for rjust()
and w3schools tutorial with examples for rjust()
Upvotes: 2