Jason Mack
Jason Mack

Reputation: 13

Formatting over a list to include commas and spacing

I am using this dictionary, appending another value to each planet with mark_planets(), and then printing out a table with defined specifications.

############### Skip this, this is just to have the formula run right
sol = {"Uranus": [2750, 3000, 2880], "Mercury": [46, 70, 57], "Earth": [147, 152, 150], "Venus": [107, 109, 108],
       "Mars": [205, 249, 228], "Saturn": [1350, 1510, 1430], "Jupiter": [741, 817, 779],
       "Pluto": [4440, 7380, 5910], "Neptune": [4450, 4550, 4500]}

status = [True, True, True, True, True, True, True, False, True]

def mark_planets(planets, stat):
    idx = 0
    for planet in planets:
        if stat[idx]:
            planets[planet].append("Planet")
            idx += 1
        else:
            planets[planet].append("Dwarf")
            idx += 1
################Below is the issue
def display_planets(sol):
    print("{:>10} {:>10} {:>15} {:>15} {:>15}".format("planet", "status", "nearest", "furthest", "average"))
    print("{:-^69s}".format("-"))
    for planet in sol:
        print("{:>10} {:>10} {:>15} {:>15} {:>15}".format(planet, sol[planet][3], sol[planet][0], sol[planet][1],sol[planet][2]))

So as shown, I have correct spacing for the output, but i need to format value[0],[1],[2] with commas between the numbers. I am not sure how to format the commas to the string and keep the correct spacing for it to print properly.

Below is the output I currently have.

    planet     status         nearest        furthest         average
---------------------------------------------------------------------
    Uranus     Planet      2750000000      3000000000      2880000000
   Mercury     Planet        46000000        70000000        57000000
     Earth     Planet       147000000       152000000       150000000
     Venus     Planet       107000000       109000000       108000000
      Mars     Planet       205000000       249000000       228000000
    Saturn     Planet      1350000000      1510000000      1430000000
   Jupiter     Planet       741000000       817000000       779000000
     Pluto      Dwarf      4440000000      7380000000      5910000000
   Neptune     Planet      4450000000      4550000000      4500000000``

Just adding the comma's to the numbers is what I am struggling with. Thanks for the help.

Upvotes: 1

Views: 45

Answers (1)

Masked Man
Masked Man

Reputation: 2538

The answer is originated from Ian Schneider's answer

You can pass a , to your integer format specification at the end so it reads like:

some_value = 12345678
"{:>15,}".format(some_value)

Upvotes: 3

Related Questions