Printing and prompting adds a mysterious extra space
mensanator at aol.com
mensanator at aol.com
Sat Oct 1 16:17:41 EDT 2005
Christoph Haas wrote:
> 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
> 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".
>>>
>huh
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
More information about the Python-list
mailing list