3

A code below:

info={'Resolution':'640x360', 'DisplayResolution': '640x360', 'Display Channels':'R,G,B,A'}
for key in info:
    print (key + str(info[key].rjust(45,'.')))

produces a following output:

Resolution......................................640x360
DisplayResolution......................................640x360
Display Channels......................................R,G,B,A

But I would like to get:

Resolution.............................................640x360
DisplayResolution......................................640x360
Display Channels.......................................R,G,B,A

How to achieve this?

EDITED:

Thanks everyone for your valuable input. Here is a summary of the code I put together based on your suggestions:

ROW_SIZE=0
for key, value in info.iteritems():
    if not key or not value: continue
    key=str(key)
    value=str(value)
    total=len(key)+len(value)+10
    if ROW_SIZE<total: ROW_SIZE=total

result=''
if ROW_SIZE:
    for key in info:
        result+=(key+str(info[key]).rjust(ROW_SIZE-len(key),'.'))+'\n'
print result
alphanumeric
  • 13,847
  • 39
  • 164
  • 305
  • 1
    possible duplicate of [fill out a python string with spaces?](http://stackoverflow.com/questions/5676646/fill-out-a-python-string-with-spaces) The function already exists. Just replace the space with a dot and you have your answer. – TheSoundDefense Aug 01 '14 at 19:27

5 Answers5

8

Place the periods as a filler for the key, not the value:

info = {'Resolution':'640x360', 'DisplayResolution': '640x360',
      'Display Channels':'R,G,B,A'}
for key, value in info.items():
    print('{k:.<55}{v}'.format(k=key, v=value))

yields

Resolution.............................................640x360
DisplayResolution......................................640x360
Display Channels.......................................R,G,B,A

The above using the newer format method. Alternatively, using old-style string formatting:

for key,value in info.items():
    print('%s%s' % (key.ljust(55, '.'), value))
unutbu
  • 711,858
  • 148
  • 1,594
  • 1,547
2

I would use a set row length and calculate the number needed for rjust like

info={'Resolution':'640x360', 'DisplayResolution': '640x360', 'Display Channels':'R,G,B,A'}
ROW_SIZE = 100 
for key in info:
    dots = ROW_SIZE - len(key) - len(info[key])
    print (key + str(info[key].rjust(dots,'.')))
Máté
  • 2,195
  • 3
  • 16
  • 25
2

Just make the rjust value dynamic to a desired total length:

For example:

info={'Resolution':'640x360', 'DisplayResolution': '640x360', 'Display Channels':'R,G,B,A'}
desired_length = 55
for key in info:
    num_dots = desired_length - len(key) + len(info[key])
    print (key + str(info[key].rjust(num_dots,'.')))

Output:

Resolution.............................................640x360
DisplayResolution......................................640x360
Display Channels.......................................R,G,B,A
agconti
  • 15,820
  • 15
  • 69
  • 108
1

the best way would be to keep track of word length and have a variable for number of spaces left and define an overall space (say 45 as length before resolution and color information)

 for key in info:
      print (key+str(info[key]).rjust(45-len(key),'.'))

One more thing, you may not need str() starting with info[key]. strings are right justified so if the output continues, it is unecessary. Otherwise perform my fix.

Andrew Scott Evans
  • 893
  • 11
  • 24
  • This is great. Exactly what I need since this approach takes into the account the variable strings length and produces a consistent result. I posted what I ended up with on `EDITED` of my original question. – alphanumeric Aug 01 '14 at 20:30
1

If you want two columns lined up even if the right column is a different width, you can do:

info={'Resolution':'640x360', 'DisplayResolution': '640x360',
      'Display Channels':'R,G,B'}

keys_col=max(len(max(info.keys(), key=len)), 35)    
val_col=max(len(max(info.keys(), key=len)), 10)
for k in info:
    v=info[k]
    line=k.ljust(keys_col, '.') + v.rjust(val_col, '.')
    print line

Prints:

Resolution...................................640x360
DisplayResolution............................640x360
Display Channels...............................R,G,B

Or, with format you can do:

for k in info:
    v=info[k]
    print '{0:.<{c0}}{1:.>{c1}}'.format(k, v, c0=keys_col, c1=val_col)
dawg
  • 80,841
  • 17
  • 117
  • 187