print() and Standard Out

Every running program has a text output area called "standard out", or sometimes just "stdout". The Python print() function takes in python data such as ints and strings, and prints those values to standard out.

alt: python program runs, each call to print() in the program append a line of text to the standard-out text area

To say that standard out is "text" here means a series of lines, where each line is a series of chars with a '\n' newline char marking the end of each line. Standard out is relatively simple. It is a single area of text shared by all the code in a program. Each printed line is appended at its end.

Standard Out in the Terminal

When you run a program from the terminal, standard out appears right there. Here is example of running the above hello.py in the terminal, and whatever it prints appears immediately in the terminal.

$ python3 hello.py
hello there
how are you?
I am fine
$

Print Function

The Python print() function takes in any number of parameters, and prints them out on one line of text. The items are each converted to text form, separated by spaces, and there is a single '\n' at the end (the "newline" char). When called with zero parameters, print() just prints the '\n' and nothing else. In the interpreter, standard-out displays to the screen in between the '>>>' prompts, so it's an easy way to see what print() does.

>>> print(12, 24, -2)
12 24 -2
>>> print('hi', 'there', -2)
hi there -2
>>> print('woot')   # 1 item, 1 \n
woot
>>> print()         # 0 items, 1 \n

>>>

In Python source code, the text of a string is written with quotes, like 'Hello', so we are very accustomed to seeing that form. Note that printing a string, just prints out the text data of the string with no quotes:

>>> print('Hello')
Hello
>>>

Print Option sep=

By default, print() separates the items by spaces. The optional sep= parameter sets a different separator text.

>>> print(12, 24, -2, sep=':')
12:24:-2
>>> print('but', 'not', 'including', sep='**')
but**not**including
>>> print('but', 'not', 'including', sep='')  # empty string
butnotincluding

Print Option end=

By default, print() puts a single '\n' after all the items. The optional end= parameter sets a custom string to appear after all the items. The most common use of this is when printing a string that already has a '\n' at its end. In that case, printing the string ends up double-spacing the output. See how the printed lines each have an extra blank line after them in this example:

>>> print('hello\n')
hello

>>> print('there\n')
there

>>>

The issue is that the string has a newline at the end, and then print() adds a second newline, so we get double spacing. The solution is to use end= to specify the empty string as the end of each line.

>>> print('hello\n', end='')
hello
>>> print('there\n', end='')
there

Print vs. Return

Thinking about the black-box design of a function, return is the main way for a function to return results back to its caller. Standard-out is a secondary way for a function to communicate data out, but it is much simpler - standard out is textual (typically), and it is shared among all the functions.

Therefore, use the return-value as the main, black-box data output and function testing mechanism. Standard-out is a secondary form of function output, most often used to produce text output for the user in the terminal.

Print To File

The super common case is printing to standard output. However, print() also works printing to an open file.

To open a file for writing, add 'w' when calling the open() function (shown below) — 'w' for "writing". This deletes any existing contents of that file, so be careful when opening for writing like this. With the file open, the optional file= parameter to print() directs the text lines into the file instead of writing them to standard output.

with open(filename, 'w') as f:
    print('Hello world', file=f)