User input is one of the fundamental mechanisms to get input to the Python applications. User input will provide some data which can be in different types into the application. The input generally read from the keyboard interactively.
The input() method can be used to take user input from keyboard. The input method will gather the input and then evaluate the input data into a Python type string. The evaluated input can be assigned into a variable. The syntax of the input() method is like below.
VAL = input(MESSAGE)
- VAL is the variable where the input will be assigned and stored.
- MESSAGE is the string text where it will be displayed to to user before input. This MESSAGE is used to provide information about the input.
In the following example, we will take the user input from the command line where it is expected to be the age of the user. The input then assigned to the variable named age.
name = input("Enter your name: ") surname = input("Enter your surname: ") age= input("Enter your age: ") country = input("Enter your country: ") print("Input types are ",type(name),type(surname),type(age),type(country))
When the input() method is executed the user input will be waiting and the execution of the application will stop until there is an input from the user or stop the process with CTRL+C. By default, the user input will be displayed on the screen after the MESSAGE and it will not be printed again. As stated previously to prevent errors all user input is converted or evaluated into the string type and assigned into the string variable. But if we need we can convert this string type into other types like integer, floating-point by using type conversion methods.
For example, when we try to use the age in order to compare with integer value we will get an error or exception like below.
age= input("Enter your age: ") if age >= 18: print("You are over 18")
We will get the following TypeError because the >= comparison operator can not be used between integer and string type.
TypeError: '>=' not supported between instances of 'str' and 'int'
We can correct this problem by casting the user input value into the variable with the int() method and then use as an integer value or variable.
age= int(input("Enter your age: ") ) if age >= 18: print("You are over 18")
Specify User Input Type
Even the input() method returns the user input as a string type there are conversion methods that can be used to convert user input into different types like integer, floating-point, etc. In the following example, we will convert the user input into different types by using the conversion methods.
name = input("Enter your name: ") #Convert User Input Into Integer age= int(input("Enter your age: ") ) #Convert User Input Into Float temprature = float(input("Enter your country: ")) #Convert User Input Into List food = input("Enter your favoured foods. Use - as separator: ") food_list = list(food.split("-"))
Python 2 raw_input() Method
If you are using Python2 you can also use the raw_input() method which is not provided with Python 3. The syntax and usage are the same as the Python3 input(). Actually, the input() is the replacement of the raw_input(). The following example will use the raw_input() method to get the user name, surname, age, and country. The raw_input() returned user input will be a string type.
name = raw_input("Enter your name: ") surname = raw_input("Enter your surname: ") age = raw_input("Enter your age: ") country = raw_input("Enter your country: ")