'generating' a print statement
David Broadwell
anti-spam.dbroadwell at mindspring.com
Thu May 8 00:53:42 EDT 2003
Do i have the wrong approach here?
using
def wideprint(temp):
format = '"
format2 = '('
for i in range(len(temp)):
format = format + "%s "
format2 = format2 + "temp["+str(i)+"],"
format = format + '"'
format2 = format2 + ')'
print "wideprint calling print with %s - %s " % (format, format2)
print format % format2
What i want to see:
>>> temp = ['some','words','here']
>>> wideprint(temp)
wideprint calling print with "%s %s %s " - (temp[0],temp[1],temp[2],)
some words here
Reality:
>>> temp = ['some','words','here']
>>> wideprint(temp)
wideprint calling print with "%s %s %s" - (temp[0],temp[1],temp[2],)
Traceback (most recent call last):
File "<pyshell#49>", line 1, in ?
wideprint(temp)
File "<pyshell#48>", line 10, in wideprint
print format % format2
TypeError: not enough arguments for format string
I'm trying to avoid using a 'kluge' construct like;
for item in range(len(temp)):
if len(temp) == 5:
print "%s %s %s %s %s" %
(temp[0],temp[1],temp[2],temp[3],temp[4])
elif len(temp) == 4:
print "%s %s %s %s" % ((temp[0],temp[1],temp[2],temp[3])
elif len(temp) == 3:
print "%s %s %s" % (temp[0],temp[1],temp[2])
elif len(temp) == 2:
print "%s %s" % (temp[0],temp[1])
elif len(temp) == 1:
print "%s" % (temp[0])
Pointers?
--
David Broadwell
More information about the Python-list
mailing list