zoukankan      html  css  js  c++  java
  • python find 返回元素的索引

    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
    1

    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')
    2

    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', 3)
    4

    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', 1, 2)
    -1

    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. 

  • 相关阅读:
    css深入理解absolute
    CSS深入理解float
    SpringBoot连接Oracle
    Oralce分页
    ps
    VUE基本安装
    JAVA运行war包
    MYSQL数据库事务隔离级别
    如何设计一个关系型数据库
    省选模拟22
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10136425.html
Copyright © 2011-2022 走看看