The print() function

When working with Python's REPL, we can enter an expression and Python prints the result. In other contexts, we must use the print() function to see results. The print() function implicitly writes to sys.stdout, so the results will be visible on the console where we ran the Python script.

We can provide any number of expressions to the print() function. Each value is converted to a string using the repr() function. The strings are combined with a default separator of ' ' and printed with a default line ending of '\n'. We can change the separator and line ending characters. Here are some examples of this:

>>> print("value", 355/113) value 3.1415929203539825 >>> print("value", 355/113, sep='=') value=3.1415929203539825 >>> print("value", ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.