Home >Backend Development >Python Tutorial >How Did Python 3 Replace `raw_input()` and What Does `input()` Do Now?

How Did Python 3 Replace `raw_input()` and What Does `input()` Do Now?

Linda Hamilton
Linda HamiltonOriginal
2024-12-09 12:54:11666browse

How Did Python 3 Replace `raw_input()` and What Does `input()` Do Now?

Python 3: Using 'input()' to Replace 'raw_input()'

In Python 2, the 'raw_input()' function was used to read input from the console without parsing it. However, in Python 3, 'raw_input()' is no longer available.

To understand the reason behind this change, we refer to the official documentation for Python 3.0:

"What's New in Python 3.0

Builtins:

  • raw_input() is renamed to input(). There are no longer separate functions for reading raw input (as bytes) and Unicode strings; there is only one function, input(), which takes a string as input and converts it to Unicode according to the encoding specified by the sys.stdin.encoding attribute. This means that there is no longer any danger of misinterpretation between text and binary data when reading input from a terminal."

Therefore, in Python 3, the 'input()' function now replaces the former 'raw_input()' function. It takes input as a string and converts it to Unicode based on the encoding specified by 'sys.stdin.encoding'. This change eliminates the potential for misinterpretation between text and binary data when reading from the terminal.

The above is the detailed content of How Did Python 3 Replace `raw_input()` and What Does `input()` Do Now?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn