Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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", 355/113, sep='=', end='!\n')
value=3.1415929203539825!

We've printed a string and the floating-point result of an expression. In the second example, we changed the separator string from a space to '='. In the third...