[Python] input() vs raw_input()

Python版本:Python 2.7.13rc1

ex1.py :

name = input("What's your name? ")
print("Nice to meet you " + name + "!")
name2 = raw_input("What's your name? ")
print("Nice to meet you " + name2 + "!")

input():

input会解释用户的输入。如果用户输入一个整数值,那么input()会返回一个整数值。在使用input()的时候,如果需要输入string, 那么需要在输入的时候加上引号。否则,Python会把这个输入当做变量来处理。

PS C:\pyex> python .\ex1.py
What's your name? John
Traceback (most recent call last):
  File ".\ex1.py", line 1, in <module>
    name = input("What's your name? ")
  File "<string>", line 1, in <module>
NameError: name 'John' is not defined

Note:尽量注意避免使用input(),因为会有安全问题。


raw_input():

raw_input不会解释输入,直接返回用户输入的内容,并不会有任何改变.。raw_input返回的是string类型,如果需要它返回整型,需要进行转换 x = int(raw_input()) 。


对比input()和raw_input()的输出结果

PS C:\pyex> python .\ex1.py
What's your name? "John\n"
Nice to meet you John
!
What's your name? "John\n"
Nice to meet you "John\n"!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值