where the extra space comes from on the stdout

Steve Holden steve at holdenweb.com
Sat Sep 30 18:09:25 EDT 2006


alf wrote:
> Hi,
> 
> I can not find out where the extra space comes from. Run following:
> 
> import os,sys
> while 1:
>      print 'Question [Y/[N]]?',
>      if sys.stdin.readline().strip() in ('Y','y'):
>          #do something
>          pass
> 
> $ python q.py
> Question [Y/[N]]?y
>   Question [Y/[N]]?y
>   Question [Y/[N]]?y
>   Question [Y/[N]]?y
>   Question [Y/[N]]?n
>   Question [Y/[N]]?
>   Question [Y/[N]]?
> 
> 
> There is a space evrywhere just before Q
> 
> Any insight?
> 
Yup. When you execute a print statement with a comma at the end it 
doesn't output the space, it simply sets a flag reminding it that there 
should be a space before the next item on the same line. If the next 
character out is a newline then the space flag is reset, but in this 
case the newline was provided by the input, so you get a space at the 
start of the next output.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list