print foo, adds a space to beginning of next line?

Alex Martelli aleaxit at yahoo.com
Wed Jun 6 17:39:16 EDT 2001


"George Young" <gry at ll.mit.edu> wrote in message
news:3B1E7D78.53A5BE30 at ll.mit.edu...
> [python 2.1, intel linux]
> def f():
>      print 'what is your name: ',
>      x=sys.stdin.readline()
>      print 'what is your age: ',
>      y=sys.stdin.readline()
>
> f()
> what is your name: mememe
>  what is your age: 48
>
> Why does it print a space at the beginning of the second line?

Looks like the .softspace attribute on sys.stdout is not being
cleared by sys.stdin.readline() [and indeed how could it be?].

You can clear it up yourself if you wish:
    sys.stdout.softspace=0
after each print and/or readline().  Or avoid ever setting it by:

> If I use sys.stdout.write('what is your name: ') instead, it
> works fine.

Yep.  Or if you use raw_input('what is your name:'), which
is designed exactly for this job, of course [but it removes
the trailing \n, which .readline() doesnt...] :-).


Alex






More information about the Python-list mailing list