[Tutor] interesting behaviour with postional output

Alan Gauld alan.gauld at btinternet.com
Mon Nov 14 20:28:06 CET 2011


On 14/11/11 18:58, 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]))	
...
> 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

The data in the file is simply a set of characters. How that is 
represented visually depends on the display device settinghs, especially 
the font.

Some fonts (like courier) are mono-spaced which means every character 
takes up the same amount of horizontal space, thus your output aligns 
correctly.

But other fonts are proportionately spaced which means than an 'i' takes 
up less room than an 'm'. This is more visually appealing for
free text but it messes up the spacing for your type of output.

If you want to display the data nicely after processing then you are 
better off using a markup language such as HTML and creating a table. 
Then it can be displayed in any browser using any font you like and the 
table structure will ensure it all lines up.

But, it's more work to create the output than simple a single print 
operation!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list