0

I have this code

print("{0:<10} {1:>25}".format("Name", "RAM"))
    for droplet in droplets:
      print("{0:<10} {1:>25}".format(droplet.name, droplet.memory))

I want it to print out a nice column like this:

Name                             RAM
example-droplet                  1024
ubuntu-s-1vcpu-1gb-nyc1-01       1024

But instead I get this:

Name                             RAM
example-droplet                           1024
ubuntu-s-1vcpu-1gb-nyc1-01                           1024

How can I get my desired output?

1
  • You ask the first field to only be 10. Should it be 25 instead? Commented May 19, 2018 at 13:56

2 Answers 2

3

You indent to the wrong side. Try this:

print("{0:<30}{1:<10}".format("Name", "RAM"))
for droplet in droplets:
    print("{0:<30}{1:<10}".format(droplet.name, droplet.memory))

which prints

Name                          RAM       
example-droplet               1024      
ubuntu-s-1vcpu-1gb-ncy1-01    1024

for me.

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

Comments

2

You can try as following

print("{0:35} {1}".format("Name", "RAM"))
for droplet in droplets:
    print("{0:35} {1}".format(droplet.name, droplet.memory))

Testing by changing droplets to map:

print("{0:35} {1}".format("Name", "RAM"))
droplets = [{'name': 'example-droplet', 'memory':1024}, {'name': 'ubuntu-s-1vcpu-1gb-nyc1-01', 'memory':256}]
for droplet in droplets:
    print("{0:35} {1}".format(droplet['name'], droplet['memory']))

Result:

Name                                RAM
example-droplet                     1024
ubuntu-s-1vcpu-1gb-nyc1-01          256

1 Comment

@eozd's answer is simpler but thank you for helping!

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.