python字符串find

Python String find() method is used to find the index of a substring in a string.

Python String find()方法用于查找字符串中子字符串的索引。

Python字符串find() (Python String find())

The syntax of find() function is:

find()函数的语法为:

str.find(sub[, start[, end]])

This function returns the lowest index in the string where substring “sub” is found within the slice s[start:end].

此函数返回在切片 s [start:end]中找到子字符串“ sub”的字符串中的最低索引。

start default value is 0 and it’s an optional argument.

起始默认值为0,它是一个可选参数。

end default value is length of the string, it’s an optional argument.

end默认值是字符串的长度,它是一个可选参数。

If the substring is not found then -1 is returned.

如果未找到子字符串,则返回-1。

We should use the find() method when we want to know the index position of the substring. For checking if a substring is present, we can use in operator.

当我们想知道子字符串的索引位置时,应该使用find()方法。 为了检查是否存在子字符串,我们可以使用in运算符。

Python字符串find()示例 (Python String find() examples)

Let’s look at some simple examples of find() method.

让我们看一下find()方法的一些简单示例。

s = 'abcd1234dcba'print(s.find('a'))  # 0
print(s.find('cd'))  # 2
print(s.find('1', 0, 5))  # 4
print(s.find('1', 0, 2))  # -1

Python字符串rfind() (Python String rfind())

Python string rfind() method is similar to find(), except that search is performed from right to left.

Python字符串rfind()方法类似于find(),不同之处在于搜索是从右向左执行的。

s = 'abcd1234dcba'print(s.rfind('a'))  # 11
print(s.rfind('a', 0, 20))  # 11
print(s.rfind('cd'))  # 2
print(s.rfind('1', 0, 5))  # 4
print(s.rfind('1', 0, 2))  # -1

查找子字符串的所有索引 (Find all indexes for substring)

Python string find() and rfind() returns the first matched index. We can define a custom function to find all the indexes where the substring is found.

Python字符串find()和rfind()返回第一个匹配的索引。 我们可以定义一个自定义函数,以查找找到子字符串的所有索引。

def find_all_indexes(input_str, search_str):l1 = []length = len(input_str)index = 0while index < length:i = input_str.find(search_str, index)if i == -1:return l1l1.append(i)index = i + 1return l1s = 'abaacdaa12aa2'
print(find_all_indexes(s, 'a'))
print(find_all_indexes(s, 'aa'))

Output:

输出:

[0, 2, 3, 6, 7, 10, 11]
[2, 6, 10]
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: str.find()

参考: str.find()

翻译自: https://www.journaldev.com/23666/python-string-find

python字符串find

python字符串find_Python字符串find()相关推荐

  1. python字符串find_Python字符串| 带示例的find()方法

    python字符串find String.find()方法 (String.find() Method) find() is an inbuilt method of python, it is us ...

  2. python 字符串 find_Python 字符串 find() 方法

    例如: 字符串中"welcome"的位置索引:txt = "Hello, welcome to my world." x = txt.find("we ...

  3. python时间日期字符串各种

    python时间日期字符串各种 python时间日期字符串各种 第一种 字符串转换成各种日期 time 库 # -*- coding: utf-8 -*- import time, datetime ...

  4. python正则取字符串日期_python 正则表达式获取字符串中所有的日期和时间

    提取日期前的处理 1.处理文本数据的日期格式统一化 text = "2015年8月31日,衢州元立金属制品有限公司仓储公司(以下简称元立仓储公司)成品仓库发生一起物体打击事故,造成直接经济损 ...

  5. 这个VS Code扩展可以自动生成Python文档字符串

    机器之心报道 编辑:魔王 该扩展利用可处理编程语言和自然语言的预训练模型 CodeBERT,实现快速生成 Python 文档字符串的功能. Visual Studio Code(简称 VS Code) ...

  6. python实现加密字符串_Python实现对字符串的加密解密方法示例

    本文实例讲述了Python实现对字符串的加密解密方法.分享给大家供大家参考,具体如下: 需求是是要将密码存在数据库里,所以要加密解密是可逆的,在数据库里不要有特殊字符,防止数据库备份和恢复中出错. 安 ...

  7. python代码统计字符串中大写字符、小写字符、特殊字符以及数值字符出现的次数

    python代码统计字符串中大写字符.小写字符.特殊字符以及数值字符出现的次数 #python代码统计字符串中大写字符.小写字符.特殊字符以及数值字符出现的次数 import restring = & ...

  8. python pandas DataFrame 字符串转日期格式

    python pandas DataFrame 字符串转日期格式 import pandas as pd import numpy as np df1 = pd.DataFrame({'日期': [' ...

  9. 超详细的Python中的字符串常识【赶紧收藏】

    今天我们说说字符串类型,涉及很多基础知识,不过字符串还是需要在多讲讲的. 回顾一下:字符串与长字符串 Python非常简单,并没有专门分出一个char(Character)类型(搞过C/Java的同学 ...

最新文章

  1. nginx error_log 错误日志配置说明
  2. DVWA--文件上传漏洞
  3. linux中/bin和/sbin和/usr/bin和/usr/sbin
  4. 如何解决JAVA环境变量配好后,重启电脑又失效的问题
  5. MFC界面库BCGControlBar v25.3新版亮点:Dialogs和Forms
  6. ios把数据传递到另一个页面_IOS 应用之间的跳转和数据传递详解
  7. 论坛用的两个函数:积分计算排名和楼层函数
  8. 利用Update Manager将ESXi 5.1 Update2升级为ESXi 6.5 Update1
  9. java前端导入excel_Java之导入Excel Vue框架前端篇
  10. 项目初始阶段、项目立项
  11. 关于zigbee协议栈各层的系统分析
  12. Linux-网络管理
  13. 西安音乐学院人计算机学院,我校在“中国大学生计算机设计大赛”中获佳绩
  14. 近期爬虫学习体会以及爬豆瓣Top250源码实战
  15. 正点原子ARM裸机开发篇
  16. JDK1.8+Spring5+SpringMVC5+Mybatis3.4项目(SMM框架)搭建
  17. v-chart是否显示lengend
  18. python 召回率_召回率与精确率
  19. unionid openid微信php,openid与unionid
  20. 企业内部沟通,即时通讯软件要怎么选?

热门文章

  1. 表迁移工具的选型-复制ibd的方法
  2. 【BZOJ 1052】 1052: [HAOI2007]覆盖问题 (乱搞)
  3. PHP合并2个数字键数组的值
  4. Python标准库05 存储对象 (pickle包,cPickle包)
  5. [转]导出数据到Excel的几种方法
  6. [转载] python学习-基础教程、深度学习
  7. POJ3764 The xor-longest Path(字典树)
  8. HDU 3400 Line belt (三分)
  9. 那些必须要知道的Javascript
  10. 1003. Parity(并查集)