print

Introduction

There are hardly any computer programs and of course hardly any Python programs, which don’t communicate with the outside world. Above a ll a program has to deliver its result in some way. One form of output goes to the standard output by using the print statement in Python. 1

>>> print "Hello User"
Hello User
>>> answer = 42
>>> print "The answer is: " + str(antwort)
The answer is: 42
>>> 

It’s possible to put the arguments inside of parentheses:

>>> print("Hallo")
Hallo
>>> print("Hallo","Python")
('Hallo', 'Python')
>>> print "Hallo","Python"
Hallo Python
>>> 

We can see that the output behaviour changes as well. But more importantly: The output behaviour of version 2.x and version 3.x is different as well, as we can see in the following:

$ python3
Python 3.2.3 (default, Apr 10 2013, 05:03:36) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello")
Hello
>>> print("Hello","Python")
Hello Python
>>> 

If you want the same output behaviour as in Python 3, you should use an import from the “future”:

Import from future: print_function

Some Python programs contain the following line:

from __future__ import print_function

This is sometimes a source of ambiguity. It looks as if we are importing a function called “print_function”. What we are doing instead is that we set a flag. If this flag is set, the interpreter makes the print function available.

We strongly recommend that you use this import, so that your programs will be compatible to the version 3 of Python. So you can go on to our version 3 introduction into the print function of our Python tutorial.


Anmerkungen:
1 Starting with version 3.0, Python doesn’t provide a print statement anymore, there is only a print function.

Sumber: https://www.python-course.eu/print.php

Leave a Reply