A trivial question about print
Ante Bagaric
abf at fly.srk.fer.hr
Thu Apr 11 09:09:53 EDT 2002
Alex Martelli <aleax at aleax.it> wrote:
>> how to make that cursed blank character dissappear?
>> Other than print "a=%d" % a because it wouldnt work for iteration of
>> prints..
>>
>> for i in range(5):
>> print i,
>>
>> gives 0, 1, 2, 3, 4 and I want 0,1,2,3,4
> Actually this gives 0 1 2 3 4 -- I don't know where you got the commas
> in the output that you seem to have observed, maybe your print statement
> was not quite as you show it here.
um, yep... I don't know where I got those commas myself :)
I wasn't copying it from the actual output, I was producing it right out
of my memory and I guess I was a little too quick. No commas there.
But the issue was that cursed blank char anyway... :)
> The print statement sets the softspace attribute on sys.stdout when it
> has just printed with a trailing comma, resets it when it emits a line
> break. If it finds the attribute set when about to continue emitting,
> it outputs a space first. The following superdirty trick works as
> expected:
>>>> import sys
>>>> for i in range(5):
> ... print setattr(sys.stdout,'softspace',0) or ("%s,"%i),
> ...
> 0,1,2,3,4,
>>>>
[...]
> but you need no trick at all to use the write method in the
> perfectly normal way within a script or module.
> Alex
Wow, pretty exhaustive answer. :)
Yet, I was thinking maybe there was something you could set in advance, to
disable printing the space; so that you could use the same, simple, print
syntax.
BTW, i never wrote a single line in Python but it looks darn attractive from
what I have read so far. Just that this print behaviour brings some
reminiscence to Fortran output formatting where some thing just happen and
there's nothing you can do about it :)
Sure in most cases this feature helps, you dont need zillion " " strings
between your variables :), but wouldnt it be nice if there was python-simple
way to just get around it?
(Not that all of this is oh-so-important, of course)
Ante
More information about the Python-list
mailing list