Python2与Python3输入语句差异详解:input与raw_input用法对比

Felix
Felix
发布时间:2026-10-13 | 2人浏览过
原创

本文详细对比Python2与Python3中输入语句的差异。Python2包含input()和raw_input()两个函数,其中input()会执行eval()处理返回类型,而raw_input()始终返回字符串。Python3仅保留input(),其行为等同于Python2的raw_input()。文章通过代码示例解析类型转换、多值输入及eval函数的具体应用,帮助开发者理解版本间的兼容性问题。

输入的概念解析

以银行ATM取款为例,用户必须输入密码才能完成操作。

程序如何获取并识别用户输入的这些数据呢?

实现ATM取款逻辑的关键步骤是:从键盘接收数据,并将其存储到变量中,这一过程直观且易于理解。

Python2中的输入语句实现

在Python2环境中,主要存在两种输入函数:input()和raw_input()。

(1)input()函数详解

该函数接收各类参数,并返回与输入内容类型一致的结果。例如,输入整数则返回int类型;若输入字符串,必须使用单引号或双引号包裹,否则程序将报错。

a.数值型输入示例

>>> a = input()
>>> type(a)
<type 'int'>
>>> a
>>> a = input()
1.23
>>> type(a)
<type 'float'>
>>> a
1.23

b.字符类型输入

若输入的字符串未添加引号,程序会抛出错误。

>>> r = input()
hello

Traceback (most recent call last):
 File "<pyshell#50>", line 1, in <module>
 r = input()
 File "<string>", line 1, in <module>
NameError: name 'hello' is not defined

正确的字符串输入方式

>>> r = input()
'hello'
>>> r
'hello'
>>> r = input()
"hello"
>>> r
'hello'

此外,可以在输入时添加提示信息。

>>> name = input('please input name:')
please input name:'Tom'
>>> print 'Your name : ',name
Your name : Tom

(2)raw_input()函数

raw_input()函数将所有输入内容视为字符串类型。输入字符串时无需加引号,若手动添加引号,引号本身也会被当作字符内容保留。

>>> a = raw_input()
>>> type(a)
<type 'str'>
>>> a
'1'
>>> a = raw_input()
'hello'
>>> type(a)
<type 'str'>
>>> a
"'hello'"

若需获取int类型数值,可调用相关转换函数进行处理。

>>> a = int(raw_input())
>>> type(a)
<type 'int'>
>>> a
>>> a = float(raw_input())
1.23
>>> type(a)
<type 'float'>
>>> a
1.23

在同一行输入多个数值有多种方式,此处展示调用map() 函数的转换方法。关于map的具体用法,可参考python-map的相关文档。

>>> a, b = map(int, raw_input().split())
20
>>> a
>>> b
>>> l = list(map(int, raw_input().split()))
2 3 4
>>> l
[1, 2, 3, 4]

(3)input()与raw_input()的核心区别

查阅input()的帮助文档可知,input()函数底层是通过调用raw_input()实现的,其额外步骤是调用了内联函数eval()。关于eval的具体用法,可参考Python eval函数妙用(详见下文)。

>>> help(input)
Help on built-in function input in module __builtin__:

input(...)
 input([prompt]) -> value
 
 Equivalent to eval(raw_input(prompt)).

>>> help(eval)
Help on built-in function eval in module __builtin__:

eval(...)
 eval(source[, globals[, locals]]) -> value
 
 Evaluate the source in the context of globals and locals.
 The source may be a string representing a Python expression
 or a code object as returned by compile().
 The globals must be a dictionary and locals can be any mapping,
 defaulting to the current globals and locals.
 If only globals is given, locals defaults to it.

Python eval函数应用解析

eval

功能:将字符串str解析为有效的Python表达式并返回计算结果。

语法:eval(source[, globals[, locals]]) -> value

参数说明:

source:一个Python表达式或compile()返回的代码对象。

globals:可选参数,必须为字典类型。

locals:可选参数,可以是任意映射对象。

实例展示:

可以把list,tuple,dict和string相互转化。
#################################################
字符串转换成列表
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>>type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> type(b)
<type 'list'>
#################################################
字符串转换成字典
>>> a = "{1: 'a', 2: 'b'}"
>>> type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
{1: 'a', 2: 'b'}
>>> type(b)
<type 'dict'>
#################################################
字符串转换成元组
>>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
>>> type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
>>> type(b)
<type 'tuple'>

Python3中的输入语句变更

Python3中仅保留input()函数,不再提供raw_input();。此外,Python3中的input()函数行为与Python2中的raw_input()完全一致。

>>> a = input()
10
>>> type(a)
<class 'str'>
>>> a
'10'

总结

以上内容涵盖了本文的核心知识点,希望能为读者的学习或工作提供参考。如有疑问欢迎留言交流,感谢支持。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表