Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable length of %s with the % operator in python

Tags:

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?

like image 406
priestc Avatar asked Sep 19 '09 15:09

priestc


2 Answers

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) 
like image 72
PaulMcG Avatar answered Oct 07 '22 15:10

PaulMcG


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.

like image 30
Esteban Küber Avatar answered Oct 07 '22 15:10

Esteban Küber