python中input和raw_input函数
python input() 相等于 eval(raw_input(prompt)) ,用来获取控制台的输入。
raw_input() 将所有输入作为字符串看待,返回字符串类型。而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )。
注意:input() 和 raw_input() 这两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。
除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。
注意:python3 里 input() 默认接收到的是 str 类型。
while True:
#input函数要求在控制台输入字串的时候必须用''括起来,不然会引发SyntaxError,纯数字不用
#python3里input()Loren接收的是str类型
reply = input('Enter text:')
if reply == 'stop':break
try:
num = int(reply)
except:
print('bad' * 8)
else:
print (int(reply) * 2)
print 'Bye'执行结果:
raw_input()实例
hint = '''funtion:
1 create new file
2 delete null file
3 delete by size
please input number:'''
while True:
option = raw_input(hint) #获取IO输入的值
if cmp(option,'1') == 0:
create()
elif cmp(option,'2') == 0:
deleteNullFile()
elif cmp(option,'3') == 0:
minSize = raw_input("minSize(K):")
deleteBySize(minSize)
elif cmp(option,'q') == 0:
print "quit !"
break
else:
print ("disabled input ,please try again....")
那如何判断控制台输入的字符串是字母字符还是数字字符:
s为字符串 s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False。 s.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False。 s.isdigit() 所有字符都是数字,为真返回 Ture,否则返回 False。 s.islower() 所有字符都是小写,为真返回 Ture,否则返回 False。 s.isupper() 所有字符都是大写,为真返回 Ture,否则返回 False。 s.istitle() 所有单词都是首字母大写,为真返回 Ture,否则返回 False。 s.isspace() 所有字符都是空白字符,为真返回 Ture,否则返回 False。
python中input和raw_input函数的更多相关文章
- Python中input()和raw_input()函数的区别
问题:在Python2.7中使用 input() 函数会出现 “NameError: Name ”***“ is not defined 的错误 解决: 使用raw_input() 函数,在Pytho ...
- python中input()和raw_input()的区别
两者均是python的内置函数,通过读取控制台的输入与用户实现交互.raw_input:将所有输入作为字符串看待,不管用户输入什么类型的都会转变成字符串. raw的 ...
- python 中 input 和 raw_input 的区别
input会假设输入的信息是合法的python表达式,例如,输入一个人名,Diesel,input会认为这是一个变量,必须加上引号,比如“Diesel”: 而raw_input会把所有的输入当作原始数 ...
- python中input()与raw_input()的区别到底是啥?-----marsggbo原创作品为你解答
首先先给出总结:input () = int(raw_input()) 有图有真相!!! 先看看input的例子: 可见用input键入得到int类型的数据,那如果输入一些非int类型的会怎么样呢? ...
- Python2中input()、raw_input()和Python3中input()
听了ALEX的某节课,说input()和raw_input()函数在Python2中没有区别,现在来探讨一下Python2中的input().raw_input()函数和Pyhont3中的input( ...
- Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted
1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...
- Python中的高阶函数与匿名函数
Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...
- python中enumerate()函数用法
python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...
- Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用
Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...
随机推荐
- 【英文文档】 Installing Go from source Go语言官方编译指南 2019.02.27
Introduction Go is an open source project, distributed under a BSD-style license. This document expl ...
- canvas.drawImage()方法详解
首先看html5.js /**@param {Element} img_elem@param {Number} dx_or_sx@param {Number} dy_or_sy@param {Numb ...
- Debugger for chrome
Debugger In VScode Getting Started Install the extension Debugger for chrome Config the launch.json ...
- golang etcdclientv3使用说明
clientv3.New() 创建连接 config = ec.Config{ Endpoints: []string{"10.0.0.5:2379"}, //连接的etcd集群地 ...
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- logging 模块 与 logging 固定模块
import logging # 1. 控制日志级别# 2. 控制日志格式# 3. 控制输出的目标为文件logging.basicConfig(filename='access.log', forma ...
- springBoot的事物管理
springBoot的事物管理 1:springBoot 整合单数据源事物: Spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务 ...
- 一.ArrayList原理及实现学习总结
一.ArrayList介绍 ArrayList是一种线性数据结构,它的底层是用数组实现的,相当于动态数组.与Java中的数组相比,它的容量能动态增长.类似于C语言中的动态申请内存,动态增长内存. 当创 ...
- 简单回射程序之处理accept返回EINTR错误的服务器程序版本
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <errno.h> ...
- lua 立即执行函数
背景 不同文件中,lua提供模块写法, 使用local修饰,可以将变量或者函数,声明为模块内有效,例如 模块暴漏变量使用 return 表的方式. local aafunc = function() ...

