By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

where the extra space comes from on the stdout

P: n/a
alf
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?

--
alfz1
Sep 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Sep 30 '06 #2

P: n/a
At Saturday 30/9/2006 19:09, Steve Holden wrote:
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
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.
You could try using
print '\rQuestion?',
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 2 '06 #3

P: n/a

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?
You already got the answer, but as for the rest: It's really easier for
you if you use raw_input() for your question/input pair instead.

Oct 2 '06 #4

P: n/a
alf
Simon Percivall wrote:
>
You already got the answer, but as for the rest: It's really easier for
you if you use raw_input() for your question/input pair instead.
thx, this is what I was looking for, alf
Dec 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.