How to take input in Python?

Taking input is a way of interact with users, or get data to provide some result. Python provides two built-in methods to read the data from the keyboard. These methods are given below.

  • input(prompt)
  • raw_input(prompt)

input()

The input function is used in all latest version of the Python. It takes the input from the user and then evaluates the expression. The Python interpreter automatically identifies the whether a user input a string, a number, or a list. Let’s understand the following example.

Example -

  # Python program showing  
# a use of input()  
  
name = input("Enter your name: ")  
print(name)  

Output:

Enter your name: Devansh
Devansh

The Python interpreter will not execute further lines until the user enters the input.

Let’s understand another example.

Example - 2

# Python program showing  
# a use of input()  
name = input("Enter your name: ")  # String Input  
age = int(input("Enter your age: ")) # Integer Input  
marks = float(input("Enter your marks: ")) # Float Input  
print("The name is:", name)  
print("The age is:", age)  
print("The marks is:", marks)  

Output:

Enter your name: Johnson
Enter your age: 21
Enter your marks: 89
The name is: Johnson
The age is 21
The marks is: 89.0

Explanation:

By default, the input() function takes input as a string so if we need to enter the integer or float type input then the input() function must be type casted.

age = int(input("Enter your age: ")) # Integer Input  
marks = float(input("Enter your marks: ")) # Float Input  

We can see in the above code where we type casted the user input into int and float.