Printing and prompting adds a mysterious extra space

C

Christoph Haas

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 program:

import sys; print ">", ; print sys.stdin.readline()

However when I run the program and enter "foobar" it looks like this:

../test.py
foobar
^----------- where does this space come from?

I wonder where the space comes from in the line where I print what the
user typed. Does it have to do with the "," after the print which I use
to suppress the newline? Any ideas?

Regards
Christoph
 
M

mensanator

Christoph said:
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 program:

import sys; print ">", ; print sys.stdin.readline()

However when I run the program and enter "foobar" it looks like this:

./test.py
foobar
^----------- where does this space come from?

I wonder where the space comes from in the line where I print what the
user typed. Does it have to do with the "," after the print which I use
to suppress the newline? Any ideas?

Another question you could ask is: why is there NO space after
the '>'? Have a look at this.

import sys

print ">",

s = sys.stdin.readline()

print len(s)
print s
for i in s:
print ord(i),i

Instead of printing the input, I'm assigning it to a variable.
Notice that the extra space appears on the next print statement
and is not part of the input. this is verified by showing that
the input has exactly 4 characters and is "huh\n".
4
huh

104 h
117 u
104 h
10


So it looks like the space was sitting in the output buffer.
Regards
Christoph
--
I'm still confused - just on a higher level now.
~
~
".signature" [Modified] 1 line --100%-- 1,48 All
 
C

Christoph Haas

Christoph said:
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 program:

import sys; print ">", ; print sys.stdin.readline()

However when I run the program and enter "foobar" it looks like this:

./test.py
foobar
^----------- where does this space come from?

Another question you could ask is: why is there NO space after
the '>'? Have a look at this.
[...]
So it looks like the space was sitting in the output buffer.

Yes, probably. But how do I get that buffer flushed? I tried
sys.stdout.flush() after the print statement but it wasn't printed,
either. I probabl need something like...

print "> "+

instead of

print ">",

How could I solve that decently?

Christoph
 
J

jepler

Use sys.stdout.write instead of print. It will solve these problems you are
having.

If you really want to know what's going on, read the language manual,
http://docs.python.org/ref/print.html It explains the behavior of this extra
space, which is output by a successive 'print' statement. The implementation
uses an attribute called 'softspace', which is described in
http://docs.python.org/lib/bltin-file-objects.html

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDPwksJd01MZaTXX0RAglOAJ4oCS76TR73IwbVGcaKJ3gZ+V9jsACgk5/i
q9c9+L8xRaY45H9phVpH9Ms=
=Egu8
-----END PGP SIGNATURE-----
 
C

Christoph Haas

Use sys.stdout.write instead of print. It will solve these problems you are
having.

If you really want to know what's going on, read the language manual,
http://docs.python.org/ref/print.html It explains the behavior of this extra
space, which is output by a successive 'print' statement. The implementation
uses an attribute called 'softspace', which is described in
http://docs.python.org/lib/bltin-file-objects.html

Thank you for the technical explanation. "print a,b" is nice to read
in simple Python programs. But when you need to get more control of
I/O it's better to know what's really happening inside.

So the "softspace" is always added after a "print a," statement but only
printed if Python thinks it is not at the beginning of a terminal line.
Interesting.

Again, thanks. I'll rest my case.

Christoph
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top