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 with multiple inputs

You can print out multiple things by calling print with multiple "inputs" each separated by a comma. For example:

>>> print('hello', 24)
hello    24

Print Option sep=

You are now venturing into "advanced" teritory. These are things you don't need to know! If you are curious, what you can do with print continues.

By default, print() separates the items by tabs. 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