python - Python 2.x 和 3.x 中输入命令的区别

标签 python text input python-3.x

好的,所以我使用了很多输入命令,而且我知道在 Python2 中我可以这样做:

text = raw_input ('Text here')

但是现在我使用 Python 3,我想知道它们之间有什么区别:

text = input('Text here')

和:

text = eval(input('Text here'))

我什么时候必须使用其中之一?

最佳答案

在 Python 3.x 中,raw_input 变成了 input 而 Python 2.x 的 input 被移除了。因此,通过在 3.x 中执行此操作:

text = input('Text here')

你基本上是在 2.x 中这样做:

text = raw_input('Text here')

在 3.x 中这样做:

text = eval(input('Text here'))

与在 2.x 中这样做是一样的:

text = input('Text here')

以下是 Python 文档的简要总结:

PEP 3111: raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).

关于python - Python 2.x 和 3.x 中输入命令的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19036188/

相关文章:

python - 安全与文件大小

iphone - iPhone 上带有白色文本和清晰背景的放大镜放大倍率

android - 如何使 aChartEngine 中的文本变大?

css - 在Safari中删除显示/隐藏密码图标

java - 解析 JTextField 中的用户输入时出现问题

python - 带有数据库回滚的路由的 Flask 功能 pytest 测试

python - Wagtail 页面标题覆盖

python - python中使用正则表达式判断变量类型

r - 如何删除引号之间的文字?

python - 从单个输入语句分配多个值,忽略空格