Programmers usually need to communicate with people to obtain information or offer a sort of outcome. Most applications nowadays employ a dialogue box to ask the user to make some form of input. At the same time, python supplies us with two inbuilt routines to examine the information from the keyboard.
In this article, we will see what are the core differences between input() and raw_input() in python.
input()
A function is a chunk of structured, reusable code used to accomplish a single, connected operation. Python contains several built-in functions; you may also develop your own. Python offers an input function that enables you to ask a user for some text input. You use this function to instruct the application to halt and wait for the user to enter the data. In Python 2, you have a built-in method raw input(); however, in Python 3, you have information () (). The software will continue whenever the user touches the ENTER or RETURN key. Look at this example to obtain input from the keyboard using Python 2 in the interactive mode. Your output is shown in quotations whenever you click the ENTER key.
# input() function in Python3.0
first_value = input("Your Name: ")
# print the type of input value
print(type(first_value))
print(first_value)
second_value = input("Your Age: ")
print(type(second_value))
second_value = int(second_value)
print(type(second_value))
print(second_value)
Output:
raw_input()
Python raw input method is applied to receive the data from the User. We call this method to tell the software to halt and trust that the User will submit the data. It is an implicit function. The input function is employed exclusively in Python 2.0 edition. Python 2.0 contains two routines to take the values from the User. The first is the input function, and another is the raw input() function. The raw input() method is similar input() function in Python 3.0. Developers are suggested to employ the natural input method in Python 2.0 since there is a flaw in the input function in Python 2.0 adaption.
val1 = raw_input("Enter the name: ")
print(type(val1))
print(val1)
val2 = raw_input("Enter the number: ")
print(type(val2))
val2 = int(val2)
print(type(val2))
print(val2)
Output:
Here, the value “python3” take from the user and saved in the val1 variable. The type of value saved is always string for raw input function. The value “1997” take from the user and keep it in the variable val2. Now, the variable val2 is a string, and we have to change the style to an integer using the int() method. The val2 variable holds the value “1997” as an integer type.