As it turns out, there is a string method named find that is remarkably similar to the function we wrote:

>>> word = 'banana'
>>> index = word.find('a')
>>> index

In this example, we invoke find on word and pass the letter we are looking for as a param- eter.

Actually, the find method is more general than our function; it can find substrings, not just characters:

>>> word.find('na')

By default, find starts at the beginning of the string, but it can take a second argument, the index where it should start:

>>> word.find('na', )

This is an example of an optional argument; find can also take a third argument, the index where it should stop:

>>> name = 'bob'
>>> name.find('b', , )
-

This search fails because b does not appear in the index range from 1 to 2, not including 2. Searching up to, but not including, the second index makes find consistent with the slice operator.

python find 返回元素的索引的更多相关文章

  1. python 返回数组的索引

    使用python里的index nums = [1, 2, 3, 4, 5, 6, 1, 9] print nums.index(max(nums)) print nums.index(1) 该方法同 ...

  2. 如何在python列表中查找某个元素的索引

    如何在python列表中查找某个元素的索引 2019-03-15 百度上回复别人的问题,几种方式的回答: 1) print('*'*15,'想找出里面有重复数据的索引值','*'*15) listA ...

  3. python之enumerate函数:获取列表中每个元素的索引和值

    源码举例: def enumerate_fn(): ''' enumerate函数:获取每个元素的索引和值 :return:打印每个元素的索引和值 ''' list = ['] for index, ...

  4. python删除列表元素remove,pop,del

    python删除列表元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me remove 删除单个元素,删除首个符合条件的元素,按值删除,返回值为空 List_remove = [1, 2, 2 ...

  5. Python列表:元素的修改、添加、删除和排序

    本文参考自<Python编程:从入门到实践>,作者:Eric Matthes,译者:袁国忠 操作 语法 举例 结果 修改元素   motocycles = ['honda', 'yamah ...

  6. python 列表删除元素,单个元素,多个连续或不连续元素

    以列表a为例 import numpy as np a = ['上海市', '云南省', '内蒙古', '四川省', '天津市', '宁夏', '安徽省', '山东省', '山西省'] 删除单个元素 ...

  7. Python创建list和按照索引访问list

    Python创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素.比如,列出班里所有同学的名字,就可以用一个list表示:>> ...

  8. Selenium with Python 003 - 页面元素定位

    WebUI自动化,首先需要定位页面中待操作的元素,然后进行各种事件操作,这里我们首先介绍Selenium Python 如何定位页面元素,WebDriver 提供了一系列的方法. 定位单个页面元素(返 ...

  9. Python - selenium_WebDriver 页面元素操作

    代码是自己写了 python WebDriver  页面操作的常用方法. from selenium import webdriver import time driver = webdriver.F ...

随机推荐

  1. linux awk时间计算脚本

    在linux如果计划时间是个麻烦事, 用awk脚本如下 BEGIN {FS=":";OFS=":"} {total_seconds=total_seconds+ ...

  2. Android 使用动画效果后的控件位置处理 类似系统通知栏下拉动画

    Android的动画的使用,请参考.Android的动画,在设计方面,我有点不太理解,觉得这样搞很怪,因为在控件动画后,即使设置了停留在动画结束时的位置,我们也确实看到了控件停在那个位置,但其实该控件 ...

  3. 三维凸包求内部一点到表面的最近距离(HDU4266)

    http://acm.hdu.edu.cn/showproblem.php?pid=4266 The Worm in the Apple Time Limit: 50000/20000 MS (Jav ...

  4. UUID的定义以及作用

    UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...

  5. SQL中distinct的用法(转载)

    1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 ...

  6. [MongoDB] 用户权限管理

    在新环境按照原来的步骤新装了MongoDB,结果出现了一些错误,才发现版本升到了2.6.1,用户权限相关的内容全部改掉了. 现在使用Role来管理用户,有一些内置的Role,也可以自定义Role. 内 ...

  7. Python爬虫scrapy-redis分布式实例(一)

    目标任务:将之前新浪网的Scrapy爬虫项目,修改为基于RedisSpider类的scrapy-redis分布式爬虫项目,将数据存入redis数据库. 一.item文件,和之前项目一样不需要改变 # ...

  8. Delphi中那些容易混淆的基础(@、^、Addr、Pointer,Move、CopyMemory,GetMem和FreeMem、GetMemory和FreeMemory、New和Dispose、StrAlloc和StrDispose、AllocMem)

    @.^.Addr.Pointer Delphi(Pascal)中有几个特殊的符号,如@.^等,弄清楚这些符号的运行,首先要明白Delphi指针的一些基础知识:指针,是一个无符号整数(unsigned ...

  9. Python开发【模块】:sqlalchemy

    一.对象映射关系(ORM) orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却 ...

  10. 以太坊geth主网全节点部署

    以太坊geth主网全节点部署 #环境 ubuntu 16.4 #硬盘500GB(目前占用200G) #客户端安装 # 查看下载页面最新版 # https://ethereum.github.io/go ...