python find用法
2013-12-05 18:10
344 查看
案例:
### 1 ###
### 2 ###
上两个案例结果都为“YES!”, 非常令我吃惊,2不应该是NO!吗?
这里注意两点:
1. if str.find('23'): 此时默认为 str.find('23') != 0:
2. find()函数找不到时返回为-1。
经查阅得知其用法:
函数原型:find(str, pos_start, pos_end)
解释:
str:被查找“字串”
pos_start:查找的首字母位置(从0开始计数。默认:0)
pos_end: 查找的末尾位置(默认-1)
返回值:如果查到:返回查找的第一个出现的位置。否则,返回-1。
举例论证:
### 1 ###
str = "01213456" if str.find("23"): print "YES!" else: print "NO!"
### 2 ###
str = "01213456" if str.find("23"): print "YES!" else: print "NO!"
上两个案例结果都为“YES!”, 非常令我吃惊,2不应该是NO!吗?
这里注意两点:
1. if str.find('23'): 此时默认为 str.find('23') != 0:
2. find()函数找不到时返回为-1。
经查阅得知其用法:
函数原型:find(str, pos_start, pos_end)
解释:
str:被查找“字串”
pos_start:查找的首字母位置(从0开始计数。默认:0)
pos_end: 查找的末尾位置(默认-1)
返回值:如果查到:返回查找的第一个出现的位置。否则,返回-1。
举例论证:
1.str = "0123" print str.find("2",1,-1) #2 2.str = "1111" print str.find("1") #0,首次出现的位置
相关文章推荐
- eclipse 安装python插件
- scikit learn环境的搭建(python27+eclipse+pydev+numpy+scipy+maplotib+scitit-learn+jdk)
- 函数:内嵌函数和闭包 - 零基础入门学习Python020
- 详解python中文编码与处理
- 谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词(转)
- Python Challenge 第四关
- Python眼睛护士改进版
- python处理二进制数据
- How Big can a Python Array Get?——python数组可以到多大?
- Python的Tkinter将窗口置顶
- python问题:IndentationError:expected an indented block错误解决
- python学习与实战
- Python3.x和Python2.x的区别
- python 2.6 与2.4的区别:
- python的日志logging模块使用总结
- [原创]利用python构造ICMP Echo Request并发送
- python批量下载图片
- python 数组的使用
- 一个简单的python网页解析+本地文件名修改的小程序
- python系列学习四——定制数据对象