Monday, July 25, 2011

Super Simple Python #3 - The print Function

You don't have to worry about filling your printer with reams of paper yet. That's the wrong kind or printing we're talking about. With the print function, you can do lots of cool things! Like sending strings to the standard console output, or sending strings to the standard error output, or even sending things to a file! How exciting!

>>> bob_string = 'Hi, my name is Bob.'
>>> print(bob_string)
Hi, my name is Bob.

It's that simple. You may notice that it doesn't have the quotation marks around it like:

>>> bob_string
'Hi, my name is Bob.'

The print function is used to display the value of something to the console (the interactive shell, IDLE, is a console). Strings by themselves won't print to the console inside of a function definition.

You can also print numbers!

>>> print(42)
42

You can even print print!!

>>> print(print)
<built-in function print>

Using print is the simplest way to get feedback from your program. Use it liberally (at least until episode #163* where we discuss the logging package).

*yeah, right. I don't even have #4 planned yet...

No comments:

Post a Comment