To use this website fully, you first need to accept the use of cookies. By agreeing to the use of cookies you consent to the use of functional cookies. For more information read this page.

Python tutorialInput and output in Python

Input and output (IO) is a major part of any programming language - being able to receive inputs and produce outputs are what makes a program interactive.

Output

Output in Python is normally achieved using the print keyword.

Python
print ("Hello world!")
    

The print method also supports multiple values which it will print in one string separated by a single space:

Python
print ("Hello", "world!")
    

Input

Input in Python varies based on the version. Version 3.x introduced the input method of getting an input from the user whereas previous versions relied on the raw_input method:

Python
# Prior to Python 3.x
print (raw_input("Please insert your name"))
# Python 3.x and later
print (str(input("Please insert your name")))
    

Notice in Python 3.x the use of the str method to convert the input to an appropriate format.

Whilst brackets are not essential with the print method, they are recommended in most cases. The only time that the brackets are not necessary is when there is only a single Python expression following it.
Provide feedback on this page
Comments are sent via email to me.