where the extra space comes from on the stdout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alf

    #1

    where the extra space comes from on the stdout

    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.readl ine().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
  • Steve Holden

    #2
    Re: where the extra space comes from on the stdout

    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.readl ine().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

    Comment

    • Gabriel G

      #3
      Re: where the extra space comes from on the stdout

      At Saturday 30/9/2006 19:09, Steve Holden wrote:
      while 1:
      print 'Question [Y/[N]]?',
      if sys.stdin.readl ine().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!


      Comment

      • Simon Percivall

        #4
        Re: where the extra space comes from on the stdout


        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.readl ine().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.

        Comment

        • alf

          #5
          Re: where the extra space comes from on the stdout

          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

          Comment

          Working...