In these tutorials, we will see how to read input from keyboard in python, in both python 2 and python 3 versions.

How to read input from the keyboard python 2:

As we discussed in the previous tutorials we can read input values from the keyboard using raw_input() function.

In Python 2 we have 2 different methods to do the same thing:

  • raw_input() and
  • input()

Why we need 2 functions for the same purpose?

The problem with this raw_input() function is : It always reads data from the keyboard in the form of String format. If we want to get the actual type, we need to convert that string type into our required type by using corresponding typecasting methods.

But in the case of input() function, it gives as we given as in input from the keyboard. Let’s see with the examples..

raw_input() Example :

x=raw_input("Enter any number: ")

The above program always gives

<class 'str'>

as output, no matter what type you are giving input.

input() Example :

x=input("Enter any number: ")

Output :

Enter any number: 10
<class 'int'>
Enter any number: Chandra
<class 'string'>

How to read input from the keyboard python 3:

In python 3: raw_input() function was renamed to input() and it always returns the type as string. And old input() function was removed.

Note: No confusion, there is no multiple functions to read input from keyboard in python 3 only one is input().

Example 1 :

Python program to read 2 numbers from keyboard and calculate the sum 

x = input("Enter First Number ")
y = input("Enter Second Number ")
a = int(x)
b = int(y)
print("The sum of given two numbers : ",a+b)

Output :

Enter First Number 10
Enter Second Number 20
The sum of given two numbers :  30

Example 2:

Python program to read Student data from keyboard and print 

stdNumber = int(input("Enter Student Number : "))
stdName = input("Enter Student Name : ")
marks = float(input("Enter percentage of marks : "))
isPass = bool(int(input("Student is pass ? [1|0] : ")))

print("Please confirm the information you have provided ? ")
print("student Number : ",stdNumber)
print("Student Name : ",stdName)
print("Percentage : ",marks)
print("is Student pass ? ",isPass)

Output :

Enter Student Number : 1001
Enter Student Name : Chandra Shekhar G
Enter percentage of marks : 26.2
Student is pass ? [1|0] : 0
Please confirm the information you have provided ?
student Number :  1001
Student Name :  Chandra Shekhar G
Percentage :  26.2
is Student pass ?  False

Example 3:

How to read multiple values from the keyboard in a single line

x,y = [int(a) for a in input("Enter any 2 numbers :").split()]
print("Product of given two numbers -: ",x*y)

Output :

Enter any 2 numbers :10 20
Product of given two numbers -: 200

Note: Here the split() function does all the stuff, it takes space as separator by default and split the single line into peace.

Example 4 :

Read any 3 floating values from the keyboard with ‘,’ as separator and calculating the sum

x,y,z = [float(a) for a in input("Enter any 3 numbers :").split(',')]
print("Sum of given three numbers -: ",x+y+z)

Output :

Enter any 3 numbers :10.5,20.5,30.5
Sum of given three numbers -: 61.5

Happy Learning 🙂

About the Author:

Founder of Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment