473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Printing and prompting adds a mysterious extra space

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?

Regards
Christoph
--
I'm still confused - just on a higher level now.
~
~
".signature" [Modified] 1 line --100%-- 1,48 All
Oct 1 '05 #1
4 1613

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


Oct 1 '05 #2
On Sat, Oct 01, 2005 at 01:17:41PM -0700, me********@aol.com wrote:
Christoph Haas wrote:
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?


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
--
I'm still confused - just on a higher level now.
~
~
".signature" [Modified] 1 line --100%-- 1,48 All
Oct 1 '05 #3
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)

iD8DBQFDPwksJd01MZaTXX0RAglOAJ4oCS76TR73IwbVGcaKJ3 gZ+V9jsACgk5/i
q9c9+L8xRaY45H9phVpH9Ms=
=Egu8
-----END PGP SIGNATURE-----

Oct 1 '05 #4
On Sat, Oct 01, 2005 at 05:09:48PM -0500, je****@unpythonic.net wrote:
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
--
I'm still confused - just on a higher level now.
~
~
".signature" [Modified] 1 line --100%-- 1,48 All
Oct 1 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert | last post by:
In the middle of an HTML document, I am adding some php code to display some text only if a variable is set. Here is my code: <? $people = $_POST; $people_fee= $people * 10; ...
7
by: jmm-list-gn | last post by:
Hello, <http://www.asaom.edu> There are some curious spacing issues with the top bar (gray) and the main navigation bar. The most significant is how the nav bar looks in Opera v6 (win2k): the...
0
by: Shawn | last post by:
Hi; I'm trying to create a method that will figure out the size of a font based on the data that is to printed to the page. i will be printing a tabular report where it's all based on two...
2
by: Jurjen de Groot | last post by:
I'm about to start a new ASP.NET application, this application will generate several types of documents (PDF HTML XML-DOC) wich have to be printed at the client-side... (probably on different...
0
by: ChrisM | last post by:
Hi, Got a strange one here. I have a report which is part of my dotnet application. It prints fine except for one thing: It adds extra spaces to certain things. eg, required text: 'Service...
9
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
8
by: dancer | last post by:
Using ASP.net 1.1 to create user input form. When I put a RequiredFieldValidator I have extra space at the bottom of the cell, as if I had put a <br>. When I comment out the...
2
by: CC | last post by:
Hi: I've conjured up the idea of building a hex line editor as a first real Python programming exercise. To begin figuring out how to display a line of data as two-digit hex bytes, I created...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.