Kevin Carruthers on 20 Oct 2004 17:48:02 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] python string formatting


Rather late but here is another way to do it:

>>> ar = = [1, 2, 3, 55, 88]
>>> print "".join(["%10d" % dd for dd in ar])
         1         2         3        55        88

I am just learning python myself.  I still tend to think in C/Java
style loops, but Pythons list transformations rock.  For a great
programmers introduction checkout:

Marc Pilgram's: www.diveintopython.org

Kevin


On Thu, 7 Oct 2004 21:25:43 -0400, Alexander Birch <abirch@gmail.com> wrote:
> On Thu, 7 Oct 2004 21:19:09 -0400, Jeff Abrahamson <jeff@purple.com> wrote:
> > I want to do something like this:
> >
> >     for(i = 0; i < 10; i++)
> >           printf("%10d", ar[i]);
> >     printf("\n");
> >
> > But I want to do it in python.
> >
> > I could say
> >
> >     for i in range(len(ar)):
> >         print "%10d" % ar[i]
> >
> > or even
> >
> >     for d in ar:
> >         print "%10d" % d
> >
> > But neither prints to a single line.
> >
> > I could build it up as a string and the print, but I don't see how to
> > format the string properly to get a table with columns.
> >
> > Any pointers?  Thanks.
> 
> What if you do:
> for d in ar:
>   print "%10d" % d,
> 
> Notice the comma at the end of line
> 
> --
> "We ought to never do wrong when people are looking."
> --Mark Twain
> 
> http://lifesabirch.org/humour/fifteen.php
> 
> 
> ___________________________________________________________________________
> Philadelphia Linux Users Group         --        http://www.phillylinux.org
> Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
> General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug
>
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug