User input is an important part of the applications where users can provide different types of data interactive during usage of the application. Python provides different ways for user input. In this tutorial, we examine how can we get the user input in Python applications.
input() for Python3
Python3 provides the
input() method in order to get user input. The input method can be used to print information about input and get all user input and return it as value. The syntax of the input() method is like below.
- TEXT is the information printed before the input action. It can be useful to provided information to the user for input.
In the following example, we get the user name as input.
name = input("Please enter your name")
The user input is returned as text and in order to get the user age and use it as a number or integer, we should use the
int() method for conversion.
age = int(input("Please enter your age"))
raw_input() for Python2
Python2 provides a different method than Python3 for user input. The
raw_input() method is used for user input in Python2. The message or information is provided as a parameter to the raw_input().
name = raw_input("Please enter your name")