newbie ``print`` question

Terry Reedy tjreedy at udel.edu
Sun Sep 2 16:49:05 EDT 2012


On 9/2/2012 3:26 PM, gwhite wrote:

> On the "rework" thing, yes, I suppose I could construct the line as a
> single string prior to print.    There would be things like `for`
> loops and conditionals to do so.  That isn't so unusual.

The usual idiom is to construct a list of pieces and then join with ''.

 >>> print(''.join(['1', '2']))
12

Or map str to a list of objects.

 >>> print(''.join(map(str, [1, 2])))
12

You can do either of these in 2.x.
If you use .write, include '\n' at the end of the list (when needed).

Print was designed as a quick and easy way to put lines of text on the 
screen. Then people asked for a way to use with with other streams, 
hence the >> hack. Then people wanted ways to control the separator and 
terminator. As that point, Guido realized that it needed to be a 
function, not a statement, with all the options requested.

-- 
Terry Jan Reedy




More information about the Python-list mailing list