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
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), Once 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 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 raw_input example
- Let us see how to use input and raw_input function in Python.
- In python there are two function to get the input from the user that is input and raw_input function. Both of these function are used to get the input from the user.
- The raw_input() function is used in python 2 version and input() function is used in Python 3 version
Example:
Here is the example of the raw_input function
new_val = raw_input("Enter name:") # raw_input function
print(type(new_val)) #python version 2.7
print(new_val)
Python raw_input timeout
Here we can check if we want to do input from the user we will take the input() function. In this example, we will give the condition if there is no input then it will display the result ‘timeout’.
Source Code:
import sys
from select import select
raw_timeout = 5
print ("Enter the string:",)
new_lis, _, _ = select([sys.stdin], [], [], raw_timeout)
if new_lis:
val = sys.stdin.readline()
print (val)
else:
print ("No string.....timeout")
In the above code first, we will import a ‘sys’ library as it is the standard way of taking input from the user. Now create a variable ‘raw_timeout’ and assign them a value.
Here is the execution of the following given code
Python raw_input default value
To get the raw_input and set the default value we can use try-except block and import readline module. In this example, the user presses the ‘enter’ keyword until the ‘Python programming’ value is store in the ‘best language’ variable.
Example:
import readline
def new_input(new_tex, fill_val=''):
readline.set_startup_hook(lambda: readline.insert_text(fill_val))
try:
return raw_input(new_tex)
finally:
readline.set_startup_hook()
new_def_val = "Python Programming"
output = new_input("language: ", new_def_val)
print("best langauge: " + output)
Here is the execution of the following given code
Python raw_input escape key
- In this example, the user enters a character and presses the ‘enter’ keyword. Now I would allow the user to exit the output by simply pressing the ‘escape’ key button on the keyboard.
- To do this task we will import a ‘pynput’ module. This module allows the user to control and monitor input devices. It contains sub-packages like keyboard and listener.
import pynput
from pynput import keyboard
def initialize():
print("Enter name: Java")
def on_press(new_key):
if new_key == keyboard.Key.esc:
return False
else:
initialize()
with keyboard.Listener(
on_press=on_press) as listener:
listener.join()
Output:
Python raw_input not working
- Raw_input is not working in Python 3 version you can use the input() function instead of raw_input(). The input function has the same functionality to take input from the user.
- If you are using the raw_input() function then install the previous version of Python that is python 2.7.
Code:
new_variable = raw_input("Enter name:")
print(type(new_variable)) # python3 version
print(new_variable)
Implementation:
As you can see that there is an error message in the output that means raw_ input is not working in Python 3.
Solution:
Python raw_input argv
- Let us see how we can use argv in Python. Basically, argv is a list of command-line arguments. To count the number of arguments in the script we can use this function.
Example:
from sys import argv
new_script, emp_name, emp_id, emp_code = argv,raw_input("Enter the employee name: "), raw_input("Enter the employee id: "), raw_input("Enter the employee code: ")
print ("script is called:", new_script)
print ("first value is:", emp_name)
print ("second value is:", emp_id)
print ("third value is:", emp_code)
Here is the execution of the following given code
Python raw_input hide password
Let us see another example of Python raw_input hide password.
Programm:
res = True
while res:
print("""(a).close!
(b). numbers!""")
res=raw_input("""Enter a Value: """)
if res == "a":
exit()
elif res == "b":
new_pas=raw_input("""Enter the Password: """)
if new_pas == "Pythonguides":
print("""(a). password (b).close""")
paswr=raw_input ("""Again Enter a Value: """)
if paswr == "a":
print ("""Password Is Spguides""")
import time
time.sleep(1)
exit()
elif paswr == "b":
exit()
else:
print("""You Have Entered the Incorrect value. Not executing the Programm""")
import time
time.sleep(1)
exit()
else:
print("""You Have Entered the Incorrect Password. Not executing the Programm""")
import time
time.sleep(1)
exit()
Here is the Screenshot of the following given code
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
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
You may like the following Python tutorials:
- Sorting algorithms in Python
- Working with JSON data in Python
- Send email using Python
- Python access modifiers + Examples
- Python Read CSV File and Write CSV File
- Python Array with Examples
- Hash table in python
In this tutorial, we learned, Python input() and raw_input() function, the key difference between the input() and raw_input() along with the below topics:
- Why we need input() and raw_input() function in python?
- Python input() function
- Python raw_input() function
- Difference between input() and raw_input() function in python
- Python raw_input example
- Python raw_input timeout
- Python raw_input default value
- Python raw_input escape key
- Python raw_input not working
- Python raw_input argv
- Python raw_input hide password
- Python NameError: name raw_input not defined
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.