tags:

views:

242

answers:

3

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?

+13  A: 

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)
Paul McGuire
+1, I didn't know you could do that
Nadia Alramli
wow, great trick! I didn't know that about the left/right padding.
Gabriel Hurley
+6  A: 

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.

voyager
+2  A: 

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
max(columns, key=len) returns an element from `columns`. The Original Poster's code gets the *length* of the longest column…
EOL
well caught, what was I thinking.
SilentGhost
why the down vote?
SilentGhost
Wasn't me, but probably because it's more pythonic to specify the width with the * specifier.
recursive
huh? more pythonic? he's printing in the loop, how is it more pythonic to use obscure syntax and expand the asterisk at each iteration?
SilentGhost