python input()与raw_input()

转自:http://blog.csdn.net/sinat_32547403/article/details/73409913

函数:raw_input()input()


注意:在python3.x中,已经删除raw_input(),取而代之的是input(),当然这仅仅是重命名,用法还是一样。因此在这里介绍的是python2.x中的raw_input()和input(),在python3.x中只要按raw_input()的使用方式就行

1:作用:读取控制台的输入与用户实现交互

2:语法
raw_input([prompt]) 
input([prompt])

3:参数
prompt:如果存在此参数,则会直接输出到屏幕上,不会再往下另起一行

4:两者关系:
input()本质上是使用raw_input()来实现的,即调用完raw_input()之后再调用eval()函数,调用如下:
def input(prompt):
    return (eval(raw_input(prompt)))

5:两者相同点:
都能接受字符串、数字以及表达式作为输入。 



6:两者差别:
6.1、当输入为字符串时:
raw_input(): 读取控制台的输入,同时返回字符串类型
input():     读取控制台的输入,但输入时必须使用引号括起来,否则会报错

6.2、当输入为纯数字时:
raw_input(): 读取控制台的输入,同时返回字符串类型,当作字符串处理
input():     读取控制台的输入,返回输入的数值类型(int, float)

6.3、当输入为字符串表达式时:
raw_input(): 读取控制台的输入,但不会对输入的数字进行运算,直接返回字符串类型,当作字符串处理
input():     读取控制台的输入,对合法的 python 数字表达式进行运算,返回运算后的结果

6.4、输入的为特殊字符时
比如'\t','\n'等
raw_input(): 读取控制台的输入,返回字符串类型,和输入一样
input():     读取控制台的输入,但输入时必须使用引号括起来,返回特殊符号所代表的内容

注:无特殊要求建议使用 raw_input() 来与用户交互



7:实例:
7.1、输入为字符串的时:
[python]  view plain  copy
  1. >>> a1 = raw_input("raw_input_str: ")  
  2. raw_input_str: hello  
  3. >>> print a1,type(a1)  
  4. hello <type 'str'>  
  5.   
  6. >>> a2 = input("input_str: ")  
  7. input_str: hello  
  8. Traceback (most recent call last):  
  9.   File "<pyshell#4>", line 1in <module>  
  10.     a2 = input("input: ")  
  11.   File "<string>", line 1in <module>  
  12. NameError: name 'hello' is not defined  
  13.   
  14. >>> a2 = input("input_str: ")  
  15. input_str: 'hello'  
  16. >>> print a2,type(a2)  
  17. hello <type 'str'>  

7.2、输入为纯数字时:
[python]  view plain  copy
  1. >>> b1 = raw_input("raw_input_int: ")  
  2. raw_input_int: 123  
  3. >>> print b1,type(b1)  
  4. 123 <type 'str'>  
  5.   
  6. >>> b2 = input("input_int: ")  
  7. input_int: 123  
  8. >>> print b2,type(b2)  
  9. 123 <type 'int'>  

7.3、输入为字符串表达式时:
[python]  view plain  copy
  1. >>> c1 = raw_input("raw_input_exp: ")  
  2. raw_input_exp: 3 + 3  
  3. >>> print c1,type(c1)  
  4. 3 + 3 <type 'str'>  
  5.   
  6. >>> c2 = input("input_exp: ")  
  7. input_exp: 3 + 3  
  8. >>> print c2,type(c2)  
  9. 6 <type 'int'>  

7.4、输入的为特殊字符时:
[python]  view plain  copy
  1. >>> d1 = raw_input("raw_input_sp: ")  
  2. raw_input_sp: \t  
  3. >>> print d1,type(d1)  
  4. \t <type 'str'>  
  5.   
  6. >>> d2 = input("input_sp: ")  
  7. input_sp: \t  
  8. Traceback (most recent call last):  
  9.   File "<pyshell#57>", line 1in <module>  
  10.     d2 = input("input_sp: ")  
  11.   File "<string>", line 1  
  12.     \t  
  13.      ^  
  14. SyntaxError: unexpected character after line continuation character  
  15.   
  16. >>> d2 = input("input_sp: ")  
  17. input_sp: '\t'  
  18. >>> print d2,type(d2)  
  19.     <type 'str'>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值