Unifying raw_input() and input() in Python 3
Question: What is the distinction between raw_input() and input() in Python 3?
Answer in Python:
# In Python 2.x: >>> type(raw_input('What is your name? ')) <type 'str'> # In Python 3.x: >>> type(input('What is your name? ')) <type 'str'>
Prior to Python 3.x, raw_input() and input() served distinct purposes. raw_input() received raw user input as a string, while input() evaluated the input as Python code.
Clarification in Python 2.x:
# Python 2.x # Get user input as a string using raw_input() name = raw_input('Enter your name: ') # Evaluate user input as Python code using input() age = input('Enter your age: ') # Returns an int
In Python 3.x, however, the distinction has been eliminated. The original raw_input() was renamed to input(). The previous input() has been removed, although it can be replicated by using eval(input()).
Caution: While eval(input()) may simulate the previous behavior of input(), it's important to exercise caution when evaluating user input as code. Eval() has inherent security risks and should be used judiciously. Safer alternatives for parsing user input should be prioritized whenever possible.
The above is the detailed content of What's the Difference Between Python 2's `raw_input()` and `input()` and How Are They Unified in Python 3?. For more information, please follow other related articles on the PHP Chinese website!