Print function and spaces
Mel Wilson
mwilson at the-wire.com
Thu Feb 5 10:58:47 EST 2004
In article <bvtfop$vs5l8$1 at ID-111250.news.uni-berlin.de>,
"Diez B. Roggisch" <nospam-deets at web.de> wrote:
>> def PrintWithoutSpaces(*args):
>> output = ""
>> for i in args:
>> output = output + i
>>
>> print output
[ ... ]
>You function won't work on mixed-type args:
>
>PrintWithoutSpaces("a", 10)
>Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 4, in PrintWithoutSpaces
>TypeError: cannot concatenate 'str' and 'int' objects
>
>
>A better way would be this:
>
>def myprint(*args):
> print "".join([str(x) for x in args])
True. Or just `output = output + str(i)` .
The `str(i)` is the vital part.
If the output string gets big, it will become plain that
`"".join`... shown above is faster.
Regards. Mel.
More information about the Python-list
mailing list