You must have created Python codes asking for input from the user. Python provides multiple ways to facilitate the user in providing the input. Two of these methods are input and raw_input. Go through this blog to understand Python input vs raw_input.
input() in python
The input() function in Python 3 fetches the input from the user in the form of a prompt. Then, it changes the user-entered input to string format. This is usually saved in a variable.
Syntax of input()
One just needs to specify the prompt after writing the input keyword. The given piece of code depicts the syntax of the input() function:
input([prompt])
Working of input()
Let’s consider an example:
n = input("Enter your name: ")
print(f"It is {n}!")
This will generate the given output:
Enter your name: Sara
It is Sara!
The accepted format is usually a string. However, you can change it to either int or float by specifying that before accepting the prompt. This function makes use of the eval() method.
age = int(input("Enter the age: "))
print(f"It is {age}!")
#this accepts integer type age
raw_input in python
Similar to the input() function, the raw_input() of Python 2 also accepts a string-based answer from the user for a particular prompt.
Syntax of raw_input()
Let’s have a look at the syntax of the raw_input() function.
raw_input([prompt])
You may comprehend this with the help of an example:
name = raw_input("Enter your nickname ")
# Print the output with name formatting
print("Hi there, {}!".format(name))
Executing this piece of code gives the following output:
Enter your nickname Sam
Hi there, Sam!
input vs raw_input
The given table draws a list of differences between the two functions: input and raw_input.
input() | raw_input() |
---|---|
It is mostly used in Python3. | It is mostly used in Python2. |
It considers the prompt in the form of an expression. | It doesn’t consider the prompt in the form of an expression. |
It doesn’t accept the prompt as a string. | It accepts the prompt as a string. |
It can be dangerous to use. | It is safer. |
Is input better than raw_input?
While input() evaluates the prompt, raw_input() takes it as an expression only. In Python 2, both work in the same manner. They don’t evaluate the expression. However, in Python 3, it is better to use raw_input() because input() accepts an integer, a float, or a string and formulates an expression. A user may enter a disruptive code, too. This is not a safe approach. Therefore, programmers prefer raw_input().
Handling input()
You might have to know that the input() function may prove to be malicious if it is not used wisely.
- In case there’s a form or any such resource wherein the user has to enter some information, try avoiding the input() function. That’s because a user may enter something that can harm the system as input() will directly evaluate it.
- Use HTML parsing in case you want to opt for input() with user-controlled software.
- You should avoid using this function with file operations, too.
Undefined raw_input
In Python 3, raw_input() doesn’t work. Python 3 supports the input() function. The given piece of code helps to check the version of Python in case you are unsure about it. The output screen displays a NameError if you use the raw_input() function with Python 3.
import sys
print(sys.version)
Usage of input()/ raw_input()
It is used for a variety of purposes:
- Calculator code, which accepts numbers for finding mathematical operations
- Guess the number of games in Python.
- File operations
- Create a menu-driven program/ command-line interpreter.
FAQS
This was done to maintain consistency. Initially, python 2 had both functions, and it created some sort of confusion.
Conclusion
This blog covers the difference between 2 widely used Python functions to accept user inputs. These are input() and raw_input(). It also discusses the syntax associated with each one of them and their usage. Besides this, it also lays emphasis on the different ways in which input() can be used efficiently.