Asked  7 Months ago    Answers:  5   Viewed   32 times

In python, if I say

print 'h'

I get the letter h and a newline. If I say

print 'h',

I get the letter h and no newline. If I say

print 'h',
print 'm',

I get the letter h, a space, and the letter m. How can I prevent Python from printing the space?

The print statements are different iterations of the same loop so I can't just use the + operator.

 Answers

95
import sys

sys.stdout.write('h')
sys.stdout.flush()

sys.stdout.write('m')
sys.stdout.flush()

You need to call sys.stdout.flush() because otherwise it will hold the text in a buffer and you won't see it.

Tuesday, June 1, 2021
 
PeterTheLobster
answered 7 Months ago
66

As I've asked about here already and found out the hard way, you aren't going to get reliable and accurate printing results purely within the browser. Even if it is an intranet application that you've been promised must only work with IE7, IE8 will be out soon and then Firefox will be allowed and all of your careful micromanagement of CSS will be for naught (do I sound bitter?).

The most forward looking solution is to bite the bullet and go for generating PDFs. The tools you mentioned are good. You should also look at iText and iTextSharp. Once you get the hang of it, doing the PDF layouts isn't any harder than HTML and CSS and you will know that the results will print correctly on everyone's computer, everyone's browser, and everyone's printer. I'm currently working with iTextSharp (not finished yet, but still learning and experimenting).

I haven't found reliable ways to control the print options from within a page either, so relying on your users to change from portrait to landscape or to set or adjust margins or turn off the print headers and footers just doesn't work in the long run - you'll end up annoying them and creating more headaches for yourself when they can't (or just don't) follow instructions.

The "Related Questions" sidebar is very useful. I saw these questions on controlling the printer from the web page (both with answers that amount to: "you can't"):

Programmatically Selecting Landscape Printing

Printing to a Specific Printer

Sunday, August 1, 2021
 
Kzqai
answered 4 Months ago
47
first, rest = l[0], l[1:]

Basically the same, except that it's a oneliner. Tuple assigment rocks.

This is a bit longer and less obvious, but generalized for all iterables (instead of being restricted to sliceables):

i = iter(l)
first = next(i) # i.next() in older versions
rest = list(i)
Wednesday, August 4, 2021
 
turson
answered 4 Months ago
87

The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes. This process is also called serializing” the object. The byte stream representing the object can then be transmitted or stored, and later reconstructed to create a new object with the same characteristics.

The cPickle module implements the same algorithm, in C instead of Python. It is many times faster than the Python implementation, but does not allow the user to subclass from Pickle. If subclassing is not important for your use, you probably want to use cPickle.

Source of above information.

Monday, September 13, 2021
 
waylaidwanderer
answered 3 Months ago
27

try:

The CSS2-friendly way to do it would be
td { page-break-inside: avoid; }

see the page-break-inside definition

from: http://channel9.msdn.com/forums/TechOff/35322-Keep-with-next-when-printing-HTML-tables/

Saturday, October 16, 2021
 
Vaishak Suresh
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share