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. 如何设置Eclipse工作区默认编辑宽度

    1)打开Window  => Preferences窗口 2)打开Formatter属性页从Java => CodeStyle => Formatter 3) 单击New创建一个自己 ...

  2. postgresql架构基础(转)-(1)

    PostgreSQL使用一种客户端/服务器的模型.一次PostgreSQL会话由下列相关的进程(程序)组成: 一个服务器进程,它管理数据库文件.接受来自客户端应用与数据库的联接并且代表客户端在数据库上 ...

  3. easyui 下对radio的选中操作

    在easyui框架下对radio 基本和没有用其的情况差不多,但是给radio赋值时不一样,不能用attr这个了,要用prop 我测试了一下 在谷歌 火狐和ie下都可以.

  4. C#知识

    2018年10月29日 1.类可以定义的位置: (1)单独定义一个class,在program类的同一个文件内 (2)单独定义一个class,在program类的不同一个文件内 (3)类内定义clas ...

  5. Servlet------>jsp EL表达式

    取值: ${data}------>pageContext.findAttribute("data"); ${data.name}------>data.getName ...

  6. 怎么将Android的API demo导入到Eclipse工作区中

    File ->New Android Project 选择Create project from existing sample (不同Android版本有对应的ApiDemo示例).

  7. 比特币BTC全节点搭建

    比特币BTC全节点搭建 #环境 ubuntu 16.4 #硬盘500GB #截止2018-12-31磁盘占用超过230GB #客户端安装 #下载页面 #https://bitcoin.org/zh_C ...

  8. 洛谷P4428二进制 [BJOI2018] 线段树

    正解:线段树 解题报告: 传送门! 话说开始看到这题的时候我想得hin简单 因为关于%3有个性质就是说一个数的各个位数之和%3=这个数%3嘛,小学基础知识? 我就想着,就直接建一棵树,只是这棵树要用个 ...

  9. java反射机制与动态代理

    在学习HadoopRPC时.用到了函数调用.函数调用都是採用的java的反射机制和动态代理来实现的,所以如今回想下java的反射和动态代理的相关知识. 一.反射 JAVA反射机制定义: JAVA反射机 ...

  10. Exhibitor(zookeeper监控工具)

    具体看github上的文档,很详细https://github.com/soabase/exhibitor/wiki/Running-Exhibitor 一. 这个是Netflix出品的一个监控工具, ...