Home >Backend Development >Python Tutorial >What's the Difference Between Python 2's `raw_input()` and Python 3's `input()`?
Differentiating Between raw_input() and input() in Python 3
Python 3 has introduced a pivotal distinction between raw_input() and input(). Unlike its predecessor, Python 3 discards the former method, consolidating raw_input()'s functionality into the redesigned input().
Evolution of Raw Input
In Python 2.x, raw_input() facilitated user input, treating it as a string value. This method was particularly useful when raw data, including special characters, needed to be gathered.
The Birth of input()
With Python 3's advent, raw_input() has been replaced by input(). The new input() retains the string-based input handling of raw_input(), but adds an additional layer of functionality: interpreting user input as Python expressions.
Emulating raw_input()
For those accustomed to raw_input(), Python 3 provides a simple solution:
input_as_string = eval(input("Enter a string: "))
By wrapping input() within eval(), user input is evaluated as a string, mimicking the behavior of raw_input(). However, it's crucial to exercise caution when using eval() due to its inherent security risks.
The above is the detailed content of What's the Difference Between Python 2's `raw_input()` and Python 3's `input()`?. For more information, please follow other related articles on the PHP Chinese website!