1

Sorry if this question has been asked before, (Yes, I've checked) but I am writing a program for Python 3.6 and having difficulty trying to align the output of columns like so:

  0,     0,  0,   // - undefined - 
 10,    94,  2,   // -----   -----
 10,  9066,  2,   // ----- ! -----
 14, 11528,  2,   // ----- " -----
 24,  9370,  3,   // ----- # -----
 22,  9501,  3,   // ----- $ -----
 29,  9632,  4,   // ----- % -----
 24,  9894,  3,   // ----- & -----
  8, 11706,  1,   // ----- ' -----

Note that the numbers do not change the position of the commas. Currently, when I try to print out like this, the columns are thrown off.

4
  • 1
    Can you add the code snippet that you tried with? Commented Jan 30, 2018 at 18:51
  • Have you looked at string padding with Python? I think this answer may help you: (use rjust instead of ljust). stackoverflow.com/a/5676676/2780645 Commented Jan 30, 2018 at 18:52
  • Currently I'm trying to figure out how to put code into my stack overflow post but sure Commented Jan 30, 2018 at 18:53
  • @jonathanking Thanks, that looks like what I needed. Should I close this question? Commented Jan 30, 2018 at 18:55

1 Answer 1

0

You can either use the method .rjust to format each piece of the string. Or use for the .format method which allows you to replace fields in a string.

my_string = "10".rjust(3)+", "+"94".rjust(7)+", "+"2".rjust(4)

print(my_string)

#or

print("{0:3d}, {1:7d}, {2:4d}".format(10, 94, 2))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.