Question about output...

Bengt Richter bokr at oz.net
Wed Oct 16 22:55:22 EDT 2002


On Thu, 17 Oct 2002 02:37:18 GMT, Grant Edwards <grante at visi.com> wrote:

>In article <m3d6q9kddt.fsf at ferret.object-craft.com.au>, Dave Cole wrote:
>
>>Tony> cout << "The average of " << firstInt << " and " << secondInt <<
>>Tony> " is " << average << endl;
>
>>>>> firstInt = 10
>>>>> secondInt = 20
>>>>> average = (firstInt + secondInt) / 2
>>>>> print 'The average of', firstInt, 'and', secondInt, 'is', average
>> The average of 10 and 20 is 15
>
>Or sys.stdout.write("The average of %d and %d is %d\n" % (firstInt, secondInt, average)
>
>The format operator "%" provides more control over how operands are
>formatted.  The format string works just like C.
>
To make things even more like C (other than the lack of ';')

def printf(fmt, *args):
    import sys
    sys.stdout.write(fmt % args)

printf("The average of %d and %d is %d\n", firstInt, secondInt, average)
Regards,
Bengt Richter



More information about the Python-list mailing list