Harsh J

Memoirs of a QWERTY Keyboard

The new print function in Python 3

6 comments

The print statement has gone. A function replaces it in Python 3.0. Apart from the other changes, this one probably needs the most mind and finger retraining, cause its something I use a lot while fiddling with the interpreter.

So far, till Python 2.6

So this is how you all have been happily typing away in Python 2.x:

print "Print this and print a newline"
print "Print this, but not a newline",

Now, if you had a collection of elements you wanted to print with certain formatting, say with punctuation, here’s how you would do it:

basket = ('Apple', 'Oranges', 'Banana')
print ", ".join(basket) + "."

# Which produces the output:
"Apple, Oranges, Banana."
About the print() function in Python 3.0

About the print() function in Python 3.0

Some would even prefer to use a logical loop instead, for readability’s sake. But this method does require a string operation to happen, and loops probably would take up lines, or worse, in certain cases.

Now, in Python 3.0 onwards

The print statement has gone for good and the print() function comes in.

This decision is well explained in the PEP (Python Enhancement Proposals) and the specific paper on it can be found here (Numbered 3105)

Digging into this function’s documentation would reveal all about it in simple text (If you are an avid reader of the Python Documentation). Of what’s continued here is all just for newbies-only, with some demonstrations.

A simple demo equivalent to the first as above:

print ("Print this line, and print a newline")
print ("Print this line, but not a newline", end="")

You might notice that its got a little complex here, with a keyword argument being supplied instead of an ending comma as before.

But lets see the full power of this new print() function by doing the same punctuation to the fruit basket as before:

basket = ('Apple', 'Oranges', 'Banana')

print (*basket, sep=", ", end=".\n")

# Which produces the same output as desired:

"Apple, Oranges, Banana."

We do the same thing as before, except that we don’t require a loop, nor string operations. The function’s two keyword arguments sep and end handle the complex jobs for us. Basically, this is what they mean:

  • sep – Seperator string – Defines the string that is to be placed between every two values printed.
  • end – Ender string – Defines the string to be printed at the end of the print function.

By default, sep has a space (‘ ‘) and end is a newline (‘ \n ‘). So a simple signature of this new print function would be like:

print ( [object(s)], sep=' ', end='\n' )

And finally, the Star of this show

The one last addition to the print function made by the Python team was the file keyword argument. That’s right, one more keyword argument.

  • file – Object Name – Specify file/object to print to.

This one is a really cool addition, and it defaults to sys.stdout, (i.e.) your terminal. Thus, a more complete print() signature is:

print ( [object(s)], sep=' ', end='\n' , file=sys.stdout)
# No, you obviously don't have to import sys for this.
# Its just to describe what file its printing to.
# (/dev/stdout in UNIX's case)

All the file argument needs is an object that supports any write(string) method.

Now lets try printing the fruit basket to a file than to the terminal as default:

fruits = open("fruitsfile.txt", "w")
basket = ('Apple', 'Oranges', 'Banana')
print (*basket, sep=", ", end=".\n", file=fruits)

# Effectively prints the punctuated line,
# to the file named fruitsfile.txt

This could have a great use, especially with logging! And since it supports any object with a write method, the possibilities could be endless.

Now tell me if you still hate that its got parentheses? Look at the power of this new function!

Written by Harsh

December 9th, 2008 at 11:06 pm

6 Responses to 'The new print function in Python 3'

Subscribe to comments with RSS or TrackBack to 'The new print function in Python 3'.

  1. I was waiting for Python 3 since long. And finally it’s here with all the goodies.

    I too personally like the all new print function.

    Anurag Bhandari

    15 Dec 08 at 10:40 pm

  2. [...] Thanks Dt for mentioning print being promoted from a statement to a function. [...]

  3. Still hate it! I also don’t see why they couldn’t keep the original print as well. They should of just made the print function an additional feature and called it printp or something.

    A language should be flexible, I don’t like being forced. And plus I don’t see why they needed to change the original at all.

    Isaiah

    17 May 10 at 5:05 am

  4. I still hate it, but could be won over. My reaction to the print and format changes are that Guido should have said “If you want Java, you know where to get it”.
    I predict that Python 4.0 will have brace syntax (with equally fervent declarations of why whitespace delimiting was always a mistake).

    OTOH, the proof of the pudding is in the eating. The real test is whether I come around after coding in it for a while.

    Paulomus

    24 Aug 10 at 3:49 am

  5. It might help learning about the uglier part of the old print statements. The redirector-like symbols, particularly. After I saw those in action somewhere, I got convinced that having print as a function object is much more pythonic.

    Harsh

    26 Aug 10 at 1:42 am

  6. @ Isaiah … “A language should be flexible, I don’t like being forced” then why are you using Python? I for one am a python newb but i already know that a big emphasis of python is “there should be one – and only one – preferable way to do it” So it is basically telling you that there should only be a single way to do everything. If you want more flexibility shouldn’t you be coding with Perl or Haskell or something like that?

    n0m4d

    27 Sep 12 at 7:19 am

Leave a Reply