Python How to read input from keyboard - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In these tutorials, we will see how to read input from keyboard in python, in both python 2 and python 3 versions.
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..
x=raw_input("Enter any number: ")
print(type(x))
The above program always gives 
<class 'str'>
 as output, no matter what type you are giving input.
x=input("Enter any number: ")
print(type(x))

Output :
Enter any number: 10
<class 'int'>
Enter any number: Chandra
<class 'string'>
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().
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
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
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.
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 🙂

Python raw_input read input from keyboard
How to Remove Spaces from String in Python
Python – How to remove duplicate elements from List
Python – Find the biggest of 2 given numbers
Python – Print different vowels present in a String
Python – How to merge two dict in Python ?
How to Read CSV File in Python
How to read a text file in Python ?
Python – How to Read Google Search Results in Selenium
Python Operators Example
Features of Python Language
Modes of Python Program
Python Number Systems Example
Python Conditional Statements
Rules to define Python Identifiers

Python raw_input read input from keyboard
How to Remove Spaces from String in Python
Python – How to remove duplicate elements from List
Python – Find the biggest of 2 given numbers
Python – Print different vowels present in a String
Python – How to merge two dict in Python ?
How to Read CSV File in Python
How to read a text file in Python ?
Python – How to Read Google Search Results in Selenium
Python Operators Example
Features of Python Language
Modes of Python Program
Python Number Systems Example
Python Conditional Statements
Rules to define Python Identifiers
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list