where the extra space comes from on the stdout

Discussion in 'Python' started by alf, Sep 30, 2006.

  1. alf

    alf Guest

    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
     
    alf, Sep 30, 2006
    #1
    1. Advertising

  2. alf

    Steve Holden Guest

    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
     
    Steve Holden, Sep 30, 2006
    #2
    1. Advertising

  3. alf

    Gabriel G Guest

    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
     
    Gabriel G, Oct 3, 2006
    #3
  4. 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.
     
    Simon Percivall, Oct 3, 2006
    #4
  5. alf

    alf Guest

    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
     
    alf, Dec 9, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,912
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,671
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,021
    Steve Lamb
    Oct 23, 2003
  4. Benjamin Rutt
    Replies:
    1
    Views:
    415
    Steve Holden
    Mar 6, 2006
  5. =?Utf-8?B?ZHc=?=
    Replies:
    5
    Views:
    1,125
    Alan Silver
    Jul 30, 2006
Loading...

Share This Page