To get data or provide a certain kind of result, developers find the need to interact with users. Dialog boxes are the most common way of asking inputs from the users. Python houses two inbuilt functions to read input from the keyboard i.e., raw_input( promt) and input ( prompt).
raw_input ():This function is used in the older version of Python like Python 2.x. It takes the input typed on the keyboard as it is, convert it to a string and returns it in the form of a variable in which we desire to store it.
# use of raw_input() in Python language
g = raw_input(“Enter your name : “)
Enter your name :PSTAnalytics
In this example, g is a variable that gets the string value that the user typed during the program execution. Enter key is used to terminate the typing of data for raw_input() function. Typecasting is used to enter numeric values.
Input ():In this function, Python automatically identifies the input by the user as string or list or a number. It takes the input from the user first and then evaluates the expression. If the user enters an invalid input, then we will see either a syntax error or an exception being raised by Python. Let’s consider the following example.
# use of input()
# in Python program
val = input(“Enter your value: “)
Enter your value: 852
Working of input function in Python:
- Program flow is stopped when input() function executes. To continue program flow a user has to give input.
- The message display or text on the output screen, which asks the user to enter input value is optional.
- Input function converts everything to a string irrespective of the type of input. If we enter an integer value then also it will get converted to a string by the input() function. Typecasting is used to convert it into integer form explicitly.
# Program to check input
# type in Python
num = input (“Enter number :”)
name1 = input(“Enter name : “)
# Printing type of input value
print (“type of number”, type(num))
print (“type of name”, type(name1))
Enter number : 741
Enter name :PSTAnalytics
type of number <class ‘str’>
type of name <class ‘str’>