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

yipeiwu_com6年前Python基础

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实现识别手写数字 Python图片读入与处理

Python实现识别手写数字 Python图片读入与处理

写在前面 在上一篇文章Python徒手实现手写数字识别—大纲中,我们已经讲过了我们想要写的全部思路,所以我们不再说全部的思路。 我这一次将图片的读入与处理的代码写了一下,和大纲写的过程一...

总结python中pass的作用

总结python中pass的作用

python中pass的作用?pass代表一个空的语句块 Python中pass的作用: 示例1,定义一个类,类中没有任何内容 保存,运行之后,该文件将报错,因为结构不完整 写入p...

python使用phoenixdb操作hbase的方法示例

今天看看怎样在 python 中使用 phoenixdb 来操作 hbase 安装 phoenixdb 库 pip install phoenixdb 例子 首先启动 que...

对Python生成器、装饰器、递归的使用详解

1、Python生成器表达式 1)、Python生成器表达式 语法格式: (expr for iter_var in iterable) (expr for iter_var in it...

python 捕获shell脚本的输出结果实例

import subprocess output =Popen(["mycmd","myarg"], stdout=PIPE).communicate()[0] import subp...