[Chicago] constant length string manipulation

Cosmin Stejerean cstejerean at gmail.com
Thu Nov 29 22:29:43 CET 2007


b.ljust(5)[:5]
This will do what you want as well.

On Nov 29, 2007 3:21 PM, Lukasz Szybalski <szybalski at gmail.com> wrote:

> On Nov 29, 2007 3:06 PM, Lukasz Szybalski <szybalski at gmail.com> wrote:
> > Hello,
> > Is there a string function in python that does the following:
> > I need a string of length 5 char, and I will pass a longer and shorter
> > string but I always need to get string of length 5.
> > If my string is longer its easy:
> > a=b[:5]
> > but if it is shorter
> > a=b[:5] will put empty space at the beginning. I need to to put the
> > space at the end.
> >
> > Is there another line of code that can do that without if statements?
> >
> > a='%5s' % b works the same as a=[b:5]
> Actually I figured it out
>
>  a='%-5s' % b will do just what i need
>
> I would expect the first one to add empty space on the right but I guess
> not.
>
> Lucas
>
> >
> >
> > Any ideas?
> > Lucas
> >
>
>
>
> --
> --
> Vim auto completion for python
>
> http://lucasmanual.com/mywiki/FrontPage#head-8ce19b13e89893059e126b719bebe4ee32fe103c
> TurboGears from start to finish:
> http://www.lucasmanual.com/mywiki/TurboGears
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>



-- 
Cosmin Stejerean
http://blog.offbytwo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20071129/65f02301/attachment.htm 


More information about the Chicago mailing list