21

I'm trying to do this:

max_title_width = max([len(text) for text in columns])

for column in columns:
    print "%10s, blah" % column

But I want to replace the 10 with the value of max_title_width. How do I do this in the most pythonic way possible?

Peter Hansen
  • 21,046
  • 5
  • 50
  • 72
priestc
  • 33,060
  • 24
  • 83
  • 117
  • 4
    In Python 2.4 and later, the []'s are unnecessary in your computation of max_title_width - they just build a list that you immediately discard after computing the max. See http://www.python.org/dev/peps/pep-0289/. – PaulMcG Sep 19 '09 at 16:08

4 Answers4

48

This is a carryover from the C formatting markup:

print "%*s, blah" % (max_title_width,column)

If you want left-justified text (for entries shorter than max_title_width), put a '-' before the '*'.

>>> text = "abcdef"
>>> print "<%*s>" % (len(text)+2,text)
<  abcdef>
>>> print "<%-*s>" % (len(text)+2,text)
<abcdef  >
>>>

If the len field is shorter than the text string, the string just overflows:

>>> print "<%*s>" % (len(text)-2,text)
<abcdef>

If you want to clip at a maximum length, use the '.' precision field of the format placeholder:

>>> print "<%.*s>" % (len(text)-2,text)
<abcd>

Put them all together this way:

%
- if left justified
* or integer - min width (if '*', insert variable length in data tuple)
.* or .integer - max width (if '*', insert variable length in data tuple)
PaulMcG
  • 62,419
  • 16
  • 94
  • 130
22

You have the new strings formatting methods from Python 3 and Python 2.6.

Starting in Python 2.6, the built-in str and unicode classes provide the ability to do complex variable substitutions and value formatting via the str.format() method described in PEP 3101. The Formatter class in the string module allows you to create and customize your own string formatting behaviors using the same implementation as the built-in format() method.

(...)

For example, suppose you wanted to have a replacement field whose field width is determined by another variable:

>>> "A man with two {0:{1}}.".format("noses", 10)
"A man with two noses     ."
>>> print("A man with two {0:{1}}.".format("noses", 10))
A man with two noses     .

So for your example it would be

max_title_width = max(len(text) for text in columns)

for column in columns:
    print "A man with two {0:{1}}".format(column, max_title_width)

I personally love the new formatting methods, as they are far more powerful and readable in my humble opinion.

Community
  • 1
  • 1
Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
  • I had no idea you could do this... At first I was going to say this felt unnatural, but after thinking about it, I guess it makes sense! – Oscar Korz Jan 30 '12 at 03:20
3

Python 2.6+ alternate version examples:

>>> '{:{n}s}, blah'.format('column', n=10)
'column    , blah'
>>> '{:*>{l}s}'.format(password[-3:], l=len(password)) # password = 'stackoverflow'
'**********low'
>>> '{:,.{n}f} {}'.format(1234.567, 'USD', n=2)
'1,234.57 USD'

Hint: first non-keyword args, then keyword args.

Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
2

you could create your template outside of the loop:

tmpl = '%%%ds, blah' % max_title_width
for column in columns:
    print tmpl % column

You could also learn about the new formatting in python.

and btw, max doesn't require a list, you can pass it an iterable:

max_title_width = max(len(i) for i in columns)
SilentGhost
  • 307,395
  • 66
  • 306
  • 293