where the extra space comes from on the stdout

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?
 
S

Steve Holden

alf said:
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
 
G

Gabriel G

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
 
S

Simon Percivall

alf said:
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.
 
A

alf

Simon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top