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. 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树

    [BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...

  2. Redis集群管理(二)

    1.进入集群客户端 任意选一个redis节点,进入redis 所在目录 cd /redis 所在目录/src/ ./redis-cli -h 本地节点的ip -p redis的端口号 -a 密码 [r ...

  3. Django - rest - framework - 下

    一.视图三部曲 https://www.cnblogs.com/wupeiqi/articles/7805382.html 使用混合(mixins) 之前得视图部分 # urls.py from dj ...

  4. importlib模块与__import__详解

    importlib模块与__import__都可以通过过字符串来导入另外一个模块,但在用法上和本质上都有很大的不同. 通过下面示例说明,有如下一个工程目录: name = 'test' def get ...

  5. Alpine Linux配置使用技巧【一个只有5M的操作系统(转)】

    Alpine Linux是一个面向安全应用的轻量级Linux发行版.它采用了musl libc和busybox以减小系统的体积和运行时资源消耗,同时还提供了自己的包管理工具apk. Alpine Li ...

  6. java 的==和equals的区别(二)

    java 的==和equals的区别 java 的==和equals的区别 ==通常表明引用的是同一个东西(引用的地址相同),equals通常表明两个对象的内容相同(值相同) ------------ ...

  7. Codeforces Round #526 (Div. 2) Solution

    A. The Fair Nut and Elevator Solved. 签. #include <bits/stdc++.h> using namespace std; #define ...

  8. 20155239 2016-2017-2 《Java程序设计》第7周学习总结

    教材学习内容总结 1.了解Lambda语言 "Lambda 表达式"(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的 ...

  9. Flux 单向数据流

    Flux 的核心就是一个简单的约定:视图层组件不允许直接修改应用状态,只能触发 action.应用的状态必须独立出来放到 store 里面统一管理,通过侦听 action 来执行具体的状态操作. 所谓 ...

  10. Git冲突:commit your changes or stash them before you can merge. 解决办法

    用git pull来更新代码的时候,遇到了下面的问题: 1 2 3 4 error: Your local changes to the following files would be overwr ...