Input in Python
Every programs need input in some form .Input can be provided from database,some other programs,mouse clicks,sensors etc,but most used method is input from keyboard.
Python provides two ways to handle the user input from keyboard:
input() and raw_input().
Input with input():
When an input() function is encountered,program execution will be stopped until input with a return key is not passed from the keyboard.input() interprets and evaluates the input which means that if user enters integer,an integer will be returned ,if user enters string,string is returned.
Syntax:
var = input(“Enter the Value”)
Code Example: name = input("What is your name? ") print "How are you ",name age = input("what is your age ") print "your age is " + str(age)
In above Program,if value 10 is entered from keyboard ,type of var will be <int>,if a list is entered,its type will <list>.While entering the string input in above program ,We need to enclose string in quotes to typecast it to string else we may encounter the following errors.
Traceback (most recent call last): File "input.py", line 1, in <module> name = input("Enter the value? ") File "<string>", line 1, in <module> NameError: name 'abcd' is not defined.
Input with raw_input():
raw_input() takes exactly what user typed and passes it back as string .It doesn’t interprets the user input.Even an integer value of 10 is entered or a list is entered its type will be of string only.We need to change the data type before using it in the program by casting it or performing eval on it.
Below program is to add two numbers by taking input from raw_input():
Raw_input() and input() in Python 3:
In python 3,raw_input() is renamed to input() so input() returns exact string.
So If you want to evaluate or interpret the user input ( input() in Python 2.x),can be manually done like:
eval(input("enter the value"))