python:中input()与raw_input()的详解


本文摘自php中文网,作者黄舟,侵删。


实验

1

2

a = input('请输入:')

print a

如果输入字符串,则马上报错:

1

2

3

4

请输入:str 

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "<string>", line 1, in <module>

但是如果输入整数,却不会报错:

1

请输入:1010

如果把 input 改成 raw_input ,则可以正常记录键盘输入的字符串:

1

a = raw_input('请输入:')print a

1

2

请输入:str

str

原因

原因就在于,input 只能接受整型输入:

1

a = input('请输入:')print type(a)

1

请输入:10<type 'int'>

raw_input 可以接受字符串输入:

1

a = raw_input('请输入:')print type(a)

1

2

请输入:str

<type 'str'>

以上就是python:中input()与raw_input()的详解的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

如何把字符串转化成时间

Python cookbook(字符串与文本)在字符串的开头或结尾处进行文本匹配操作

Python代码画出爱心曲线

如何列出一个目录的所有文件

Python数据挖掘需要学什么

Python如何把列表变字典

Python软件收费吗

Python egg怎么安装

实例详解Python使用回溯法子集树模板获取最长公共子序列问题

爬虫Python什么意思

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...