您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python读取键盘输入的2种方法

2024/4/11 22:41:53发布17次查看
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]
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product