0

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.

1
  • 1
    Maybe something like len(num) * " " i.e. len(num) * space Commented May 6, 2020 at 18:50

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much...you're a life saver. this will be very helpful; appreciate it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.