Displaying Output

Now after installing your Python IDLE we are ready to write our first program. This program is not going to do much but its good for beginning.

print() function

print() is a function that is used to display output to the screen.

If you are not aware of functions then don't panic. You will learn about functions in upcoming lessons. For now just remember that anything having parenthesis is a function and inside parenthesis we can pass arguments.

Syntax for using print() :

print(value, ..., sep=' ', end='\n')

Here's our first program :

>>>print('Hello World!')
output: Hello World

Whatever value you provide inside print its going to display that. In this example we are passing string as a value.

Try few more examples:

>>>print(1)
output: 1
>>>print(1+2)
output: 3

You can pass more than one value inside print. It will display them with spaces in between them.

>>>print('hello', 'world')
output: hello world

Now print() function has two optional parameters, sep and end.

Lets see sep first. sep is the separator and by default its a space. You can change this behaviour by passing a sep parameter.

Try this examples :

>>>print('Hello', 'World', sep=' * ')
output:Hello * World

end parameter decides how to end the line. By default its value is '\n' which means a line break.

To understand it better, in your IDLE click on 'File' and then 'New File'. Or you can use shortcut key 'ctrl+n'. IDLE editor will get open. IDLE editor is used for writing big program.

Now write the following line:

print('Hello')
print('World')

Save it as hello.py and press F5 or click on 'Run' and then 'Run Module'. You will get the output as follows:

Output:
Hello
World

Now edit the code as follows:

print('Hello', end=' ')
print('World', end=' ')

Save it and run the code by hitting F5 button.

Output:
Hello World

You can pass both parameters together but the order should be maintained that is first value, then sep parameter and the end parameter. Experiment with the concepts that you have just learned.

<< Introduction to Python Variables and Data Types >>