[Tutor] Does Python have anything like Perls format output?

Alan Gauld alan.gauld at freenet.co.uk
Sun Apr 24 09:54:03 CEST 2005


> Good evening!  Does Python have a print function similar to Perl
> format output (example below)?
>        format STDOUT =
>        @<<<<< @>>>> @<<<<<<<<<<<<<<<<<<< $@######.## $@######.##
>        $code, $date,$descript,        $amt,       $balance

This should be in a FAQ somewhere because it comes up a lot!

There is no direct equivalent but very similar effects are achieved 
using triple quoted format strings with named parameters:

format = '''
%-5(code)s %4(date)s %-20(descript)s $%9.2(amt)f $%9.2(balance)f'''

Then when you print the string provide the builtin local variables 
dictionary as the feeder for the variable substitution.

PS. 
The above string may not be exactly the same as your example 
- my Perl memory is fading fast...

HTH,

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