[Tutor] interesting behaviour with postional output

Joel Goldstick joel.goldstick at gmail.com
Mon Nov 14 22:16:27 CET 2011


On Mon, Nov 14, 2011 at 2:26 PM, Chris Fuller <cfuller084 at thinkingplanet.net
> wrote:

>
> Fonts can be categorized as "fixed" or "proportional" widths.  Proportional
> width fonts have different widths depending on the character.
>
> If you want to line up text, you must use fixed width fonts like courier,
> or
> use tabs, but that can be unreliable.  You can get fancy and measure the
> width
> of a string in some font (if you have an API that allows it), but that is
> rarely worthwhile.
>
> Cheers
>
> On Monday 14 November 2011, Cranky Frankie wrote:
> > I'm working on writing out postional output. When I use this statement:
> >
> > print('%-40s%s' % (element_list[0], element_list[1]))
> >
> > I get nice, lined up columns on the screen. When I write to a file like
> > this:
> >
> > new_line = ('%-40s%s%s' % (element_list[0], element_list[1],'\n'))
> > file_out.write(new_line)
> >
> > a very funny thing happens. When I open the file in Notepad, the
> > columns are not lined up, however when I switch the font to Courier,
> > they * are* lined up. So what's happening is the output statement is
> > working correctly - the second column starts in the 41st postion - but
> > you have to use a non-proportional font to see it.
> >
> > I was hoping to not have the font be an issue.
> >
> > Very interesting.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Tabs will cause several kinds of problems.  First, tabs on lines with other
characters will do the same thing as spaces will do.  The same problem will
exist.  Second, if tabs are set for say every 4 spaces, and you have 3
characters of text in your first column, then tab to will go to the 8th
column.  If you have 8 characters of text, you will go to the 12 column.
So they won't line up even if you use a mono spaced font.

So, as was stated, best to use mono space if you are writing to a
terminal.  If you are writing web pages, of course you have lots of
formatting options

-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111114/0866fd43/attachment.html>


More information about the Tutor mailing list