栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 查找字符串内所有字符起始位置

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 查找字符串内所有字符起始位置

 定义:

def strfindall(zstr, xstr):
    """
    查找字符串内所有字符起始位置n
    :param zstr: 被查找的字符串容器
    :param xstr: 要查找的字符串
    """
    xnum = 0
    for i in range(zstr.count(f'{xstr}')):
        xnum = zstr.find(f'{xstr}', xnum)
        print(xnum, end=',')
        xnum += len(xstr)

 测试:

abc = '''This do cument rep orts on the results of an automatic security scan. All dates are dis-
played using the timezone "Co ordinated Universal Time", which is abbreviated "UTC". The
task was "every port". The scan started at Fri Oct 29 01:02:17 2021 UTC and ended at
Fri Oct 29 04:04:45 2021 UTC. The rep ort first summarises the results found. Then, for
each host, the rep ort describ es every issue found. Please consider the advice given in each
description, in order to rectify the issue.'''

strfindall(abc, 'i')
# 输出 2,52,60,85,98,107,122,131,140,148,152,161,223,265,306,317,379,391,415,427,432,437,450,453,458,474,482,
截图:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/655584.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号