printing columns question

Tim Roberts timr at probo.com
Fri Mar 30 02:52:11 EST 2001


"Victor Louie" <vlouie at telusplanet.net> wrote:
>
>...i was just wondering if anybody could explain to me how the print
>worked...
>    ie. print '\t%-15s%-15s%4d' % (prev, i, citymap[prev][i][0]),
>
>I understand that \t is a tab....but i don't get anything after that

%s says to suck a string out of the parameter tuple and insert it here.
%15s says to right-justify the string in a field 15 columns wide.
%-15s says to left-justify the string in a field 15 columns wide.

  for s in ('x','xx','xxx','xxxx'):
    print "%15s***%-15s***" % (s,s)

              x***x              ***
             xx***xx             ***
            xxx***xxx            ***
           xxxx***xxxx           ***

With %15s or %-15s, the ENTIRE string is inserted, even if it is longer
than 15 characters.  If you want the string limited to at MOST 15
characters, use "%-15.15s".
--
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list