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批量解压zip文件的方法

这是一个用python写解压大量zip脚本的说明,本人新手一个,希望能对各位有所启发。 首先要注意的,在运行自己的脚本之前一定先备份或者复制出一些样本进行测试,不然出错会很麻烦; 之后我...

python thread 并发且顺序运行示例

复制代码 代码如下:#-*- coding:utf-8 -*- import threading import time def fun(name, ls_name, front_thr...

使用PDB模式调试Python程序介绍

以前在windows下一直用的idel带的功能调试python程序,在linux下没调试过。(很多时候只是print)就从网上查找一下~ 方法: 复制代码 代码如下: python -m...

使用Python获取网段IP个数以及地址清单的方法

使用Python获取网段IP个数以及地址清单的方法

使用Python获取网段的IP个数以及地址清单需要用到IPy的库,而相应的方法主要就是IP。 写小脚本如下: from IPy import IP ip = IP('192.1...

独特的python循环语句

1、局部变量 for i in range(5): print i, print i, 运行结果: 0 1 2 3 4 4 i是for语句里面的局部变量。但在python...