Python读取键盘输入的2种方法,python键盘输入2种
Python读取键盘输入的2种方法,python键盘输入2种
Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘。如下:
1.raw_input
2.input
raw_input函数
raw_input() 函数从标准输入读取一个行,并返回一个字符串(去掉结尾的换行符):
复制代码 代码如下:
str = raw_input("Enter your input: ");
print "Received input is : ", str
这将提示你输入任意字符串,然后在屏幕上显示相同的字符串。当我输入"Hello Python!",它的输出如下:
复制代码 代码如下:
Enter your input: Hello Python
Received input is : Hello Python
input函数
input() 函数和raw_input() 函数基本可以互换,但是input会假设你的输入是一个有效的Python表达式,并返回运算结果。这应该是两者的最大区别。
复制代码 代码如下:
str = input("Enter your input: ");
print "Received input is : ", str
这会产生如下的对应着输入的结果:
复制代码 代码如下:
Enter your input: [x*5 for x in range(2,10,2)]
Recieved input is : [10, 20, 30, 40]
相关内容
- Python实现比较两个列表(list)范围,pythonlist
- Python 3.x 新特性及10大变化,python3.x
- 搞笑的程序猿:看看你是哪种Python程序员,python程序员
- Python中unittest模块做UT(单元测试)使用实例,pythonun
- Windows下实现Python2和Python3两个版共存的方法,python2py
- Python2中的raw_input() 与 input(),python2raw_input
- 在Linux下使用Python的matplotlib绘制数据图的教程,python
- Python语言实现机器学习的K-近邻算法,pythonk-
- 在Linux系统上安装Python的Scrapy框架的教程,pythonscrapy
- python中的代码编码格式转换问题,python格式转换
评论关闭