472,374 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

where the extra space comes from on the stdout

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
4 1386
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Hunsley | last post by:
Very very simple problem here, can't find out how to do it... I want to print something without a new line after it, so I can later output something right after it on the same line. If I do: ...
3
by: Jon Perez | last post by:
I'd like to print a series of successive periods as a progress status in a command line script using python. Unforutnately, if I do a series of successive print ".", I get a space in between....
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
4
by: Christoph Haas | last post by:
Evening... I'm writing a simple interactive program to maintain a database. The goal was to print "> " at the beginning of the line, wait for user input and then deal with it. Minimal test...
7
by: Britney | last post by:
Original code: this.oleDbSelectCommand1.CommandText = "SELECT TOP 100 user_id, password, nick_name, sex, age, has_picture, city, state, " + "country FROM dbo.users WHERE (has_picture = ?) AND (sex...
6
by: HappyHippy | last post by:
More of a minor niggle than anything but how would I remove the aforementioned space? eg. strName = 'World' print 'Hello', strName, ', how are you today?' comes out as "Hello World , how are...
1
by: Benjamin Rutt | last post by:
There has been a problem that has been bugging me for a while for reading input from standard in. Consider the following simple program: #!/usr/bin/env python import sys print 'enter...
8
by: dancer | last post by:
Using ASP.net 1.1 to create user input form. When I put a RequiredFieldValidator I have extra space at the bottom of the cell, as if I had put a <br>. When I comment out the...
0
by: arkascha | last post by:
Helloooo everyone, I have a problem with a small solution I made some three or four years ago. Worked flawless until deployed onto newer machines now and guess what, I cannot fix it. Maybe some...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.