On 23 Jun 2005 21:27:20 -0700, "Paul McGuire" <[EMAIL PROTECTED]> wrote:
>Dang, that class should be: > >class PaddedStr(str): > def __new__(cls,s,l,padc=' '): > if l > len(s): > s2 = "%s%s" % (s,padc*(l-len(s))) > return str.__new__(cls,s2) > else: > return str.__new__(cls,s) > Or you could write >>> class PaddedStr2(str): ... def __new__(cls,s,l,padc=' '): ... return str.__new__(cls, s+padc*(l-len(s))) ... Which gives >>> print '>%s<' % PaddedStr2('xxx',5,'.') >xxx..< >>> print '>%s<' % PaddedStr2('xxx',3,'.') >xxx< >>> print '>%s<' % PaddedStr2('xxx',2,'.') >xxx< (Taking advantage of multipliers <=0 working like 0 for strings): >>> for i in xrange(-3,4): print '%2s: >%s<'% (i, 'xxx'+'.'*i) ... -3: >xxx< -2: >xxx< -1: >xxx< 0: >xxx< 1: >xxx.< 2: >xxx..< 3: >xxx...< Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list