473,804 Members | 3,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1633

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

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

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

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.flus h() 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.writ e 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)

iD8DBQFDPwksJd0 1MZaTXX0RAglOAJ 4oCS76TR73IwbVG caKJ3gZ+V9jsACg k5/i
q9c9+L8xRaY45H9 phVpH9Ms=
=Egu8
-----END PGP SIGNATURE-----

Oct 1 '05 #4
On Sat, Oct 01, 2005 at 05:09:48PM -0500, je****@unpython ic.net wrote:
Use sys.stdout.writ e 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
1783
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; $people_text='<tr bgcolor="#ffffff"> <td valign="top"width="25%"><font size="2" color="black"
7
6241
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 text portion is has extra space at the top that I cannot remove. As a result the text portion is pushed below the background bar by a few pixels. This does not occur in Mozilla/Firefox, or IE6. (I have ordered Opera v7.) Is this a known issue? The...
0
237
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 rectangles the first on will contain a three char word and the second cell will have up to 44 characters. there will be X number of these pairings horizontally. Since i don't want the cells to vary in width i did the calculations based on a width of 44...
2
2898
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 printers A3 and A4 type) At this time I'm trying to figure out what would be the best way to accomplish this : 1. printing through HTML in a IE windows will result in page title & nr as header and URL + Date as footer, wich is far from acceptable....
0
1302
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 Bulletin HC-SB-61-269' printed text: 'Service Bulletin HC -SB-61-269' All looks fine on the screen (print preview) but the spaces appear when I actually print.
9
3021
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 accordingly ? I have this doubt as different pointers may have different representation and different size. So, how does printf determine
8
5916
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 clients (Internet Explorer). My flash content was originally brought in via the “flash satay” method, but I have since used some server-side magic do deliver one <objecttag
8
2000
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 RequiredFieldValidator, the extra space goes away. Don't want that extra space, but want the validator. What can I do?
2
2328
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 a hunk of data then printed it: ln = '\x00\x01\xFF 456789abcdef' for i in range(0,15):
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.