[Tutor] [newbie] output formatting

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Nov 5 15:36:52 EST 2003


> Now I would like the 9.8 printed either as 09.8 or with an extra
leading space.
> ...
> but it doesn't work:
>
> >print '%02.2f, %02.2F'% (T1, T2)
> >3.14, 3.14
>
> Life is hard for Python newbies :-(

The first format figure 02 gives the *ytotal* length of
the printed number, the second .2 gives how many digits
after the point.

THus 2.2 implies 2 characters with 2 digits after the point.
But .2 is already 3 characters so Python will always ignore
the first 02.

You need at least 5 characters 2+'.'+2 so you want:

'05.2f'

> >for i = 1 to 10 do
>
> is it really true that I should do
>
> >for i in range(1,10+1)

Yes, because Python's "for" is really a "Foreach", and so it
needs a sequence of items. range() generates such a sequence.

Try my tutor, which explains all of the above and more! :-)

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list