Python input and raw_input function

By: On:

In this python tutorial, you will learn about Python input() and raw_input() function, the difference between the input() and raw_input() with examples, and also we will see python NameError: name raw_input not defined.

Why we need input() and raw_input() function in python?

The main reason is the user-friendly code which is interactive. It makes the code interactive instead of hard coding. We use input() and raw_input() function to accept the user inputs according to their own value.


Python input() function

The python input() function takes the value from the user. This function is called to tell the program to stop and wait for the user to input the values. It reads the input and returns the python type like int, list, tuple, etc.

Example: Program in python2

value = input("Enter your name:")
print(type(value)
print(value)

After writing the above code (python input() function), Ones you will print “ value ” then the output will appear as a “Enter your name: Naisha <class ‘str’> Naisha “. Here, it asks the user to enter the value, and then it reads the input and returns the type of input the user entered. We don’t need to explicitly change the variable type in python2.

You can refer to the below screenshot python input() function


Python input() function
Python input() function

The Python input() function is used in both the version of python 2.x and 3.x. In Python 3.x the input function explicitly converts the input you give to type string otherwise the value stored is always string for input function in python3. But in python 2.x the function takes the value and the input you enter will take as it is without modifying its types.

Example: Program in python3

value = input("Enter the roll: ")
value = int(value)
print(type(value)
print(value)

After writing the above code (python input() function), Ones you will print “ value ” then the output will appear as a “Enter your roll: 23065 <class ‘int’> 23065 “. Here, it asks the user to enter the roll, and then it reads the input and returns the type of input the user entered. Also, we specify the type of variable as an integer.

You can refer to the below screenshot python input() function

Python input function
Python input() function

Python raw_input() function

Python raw_input() function reads the input and returns a string. It is used to get value from the user. This input function is used only in the Python 2.x version.

Python 2.x has two functions to take the value from the user. The first one is input and the second is the raw_input function. The raw_input() function is similar to input() function in python3.

Example: Program in python2

value = raw_input("Enter your name: ")
print(type(value))
print(value)

In the output, you can see its type is a string. The type of value stored is always string for raw_input() function.

Output:

Enter your name: Trisha
<type 'str'>
Trisha

Here, in python2 we will see if we want the output in integer then, we have to convert the type to an integer using “int” in raw_input() function.

Example:

value = raw_input("Enter the roll: ")
value = int(value)
print(type(value)
print(value)

Here, the value “20564” is taken from the user but we have converted the type to an integer using int(). Once you will print “value” then you will get the variable and its type. You can see the below output.

Output:

Enter the roll: 20564
<type 'int'>
20564

Difference between input() and raw_input() function in python

Let us discuss the difference between input() and raw_input() function in python.

  • The raw_input function was built in python 2. But in python3 we don’t have.
  • The raw_input function work in the same way as the input function in python3.
  • In python 2, the input() function was used first to take raw_input() and then performing an eval() in it.
  • In python 2, raw_input() returns a string whereas input() return result of an evaluation. While in python 3 input() returns a string but can be converted to any type.

Python NameError: name raw_input not defined

We get this name error because of the python version. In python3.x, input() replaces raw_input(), for input from the console. So we get this error name ‘raw_input’ not defined in python3.

Example:

value = raw_input("Enter the name: ")
print(type(value))
print(value)

After writing the above code (python nameerror: name raw_input not defined), Ones you will print “ value ” then the error will appear as  “NameError: name raw_input not defined“. Here, we get this error because it doesn’t have raw_input() in python3.

You can refer to the below screenshot for python nameerror: name raw_input not defined

Python NameError name raw_input not defined
Python NameError: name raw_input not defined

To solve this error we can use input() instead of raw_input() in python3 to avoid this error. Otherwise, if you want to use the raw_input() function then we have python2.

Example:

value = input("Enter the name: ")
print(type(value))
print(value)

After writing the above code, Once you will print “ value ” then the output will appear as an “Enter the name: John <class ‘str’> John “. Here, the error is solved by using the “input()” function in python3 which works similarly to raw_input() in python2.

You can refer to the below screenshot name raw_input not defined

Python NameError: name raw_input not defined
Python NameError: name raw_input not defined

You may like the following Python tutorials:

In this tutorial, we learned, Python input() and raw_input() function, the key difference between the input() and raw_input() along with examples, and also we have seen python NameError: name raw_input not defined.

Leave a Comment