raw_input() function
Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function. The input function is used only in Python 2.x version. The Python 2.x has two functions to take the value from the user. The first one is input function and another one is raw_input() function. The raw_input() function is similar to input() function in Python 3.x. Developers are recommended to use raw_input function in Python 2.x. Because there is a vulnerability in input function in Python 2.x version.
Example program in Python2
Python3
# Python program to demonstrate # input() function in Python2.x 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) |
Input and Output
Here, the value “python3” take from the user and store it in the val1 variable. The type of the value stored is always string for raw_input function. The value “1997” take from the user and store it in the variable val2. Now, the type of variable val2 is a string and we have to convert the type to an integer using int() function. The val2 variable stores the value “1997” as an integer type.
Let us see the differences in a tabular form -:
input() | raw_input() | |
1. | input() function take the user input. | raw_input() function takes the input from the user. |
2. |
Its syntax is -: input(prompt) |
Its syntax is -: raw_input(input) |
3. | It takes only one parameter that is prompt. | It takes only one parameter that is the input. |
4. | It return the input that it takes. | Its return type is of string. |
5. | It converts the input into a string by removing the trailing newline | It is only introduced in python 2.0 version |
6. | blocks until input received | hang ’till user inputs |
7. | “hello world” | “hello world” but string |
8. | foo | in snake_case |
Difference between input() and raw_input() functions in Python
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard.
- input ( prompt )
- raw_input ( prompt )
Contact Us