How to print to the Screen using Python?

PythonServer Side ProgrammingProgramming

The basic way to do output to screen is to use the print statement.

>>> print 'Hello, world'
Hello, world

To print multiple things on the same line separated by spaces, use commas between them. For example:

>>> print 'Hello,', 'World'
Hello, World

While neither string contained a space, a space was added by the print statement because of the comma between the two objects. Arbitrary data types can also be printed using the same print statement, For example:

>>> import os
>>> print 1,0xff,0777,(1+5j),-0.999,map,sys
1 255 511 (1+5j) -0.999 <module 'os' (built-in)>

Objects can be printed on the same line without needing to be on the same line if one puts a comma at the end of a print statement:

>>> for i in range(10):
...    print i,
0 1 2 3 4 5 6 7 8 9

To end the printed line with a newline, add a print statement without any objects.

for i in range(10):
    print i,
print
for i in range(10,20):
    print i,

The output will be:

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19

If the bare print statement were not present, the ouput would be on a single line.

In Python 3, all print statements arguments need to be surrounded by parenthesis. For example,

>>> print("Hello", "world")
Hello world
raja
Published on 21-Dec-2017 12:29:13
Advertisements