python如何寻找主串中所有指定子串下标

 更新时间:2023年01月03日 10:52:55   作者:头秃头凉凉  
这篇文章主要介绍了python如何寻找主串中所有指定子串下标,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

python寻找主串中所有指定子串下标

该函数可实现显示字符串中指定子串所有下标(首字下标)

def subStrIndex(substr,str):
    result = []
    index = 0
    while str.find(substr,index,len(str)) != -1:
        temIndex = str.find(substr,index,len(str))
        result.append(temIndex)
        index = temIndex + 1
    return result

其中substr中传入需要的寻找子串,str为主串。

使用示例:

str = "我们去了天安门,天安门附近有很多人"
list = subStrIndex('天安门',str)
print(list)

输出结果:[4,8]

其中4表示第一次出现“天安门”的下标,8表示第二次出现的下标。(由0开始)

python字符串常用操作

查找

1、find():检测某个⼦串是否包含在这个字符串中,如果在,返回这个串开始的位置下标,否则则返回-1。

语法:字符串串序列列.find(⼦子串串, 开始位置下标, 结束位置下标)

mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1

2、index():检测某个⼦串是否包含在这个字符串中,如果在返回这个子串开始的位置下标,否则报异常。

语法:字符串串序列列.index(⼦子串串, 开始位置下标, 结束位置下标)

mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错
  • rfind(): 和find()功能相同,但查找⽅方向为右侧开始。
  • rindex():和index()功能相同,但查找⽅方向为右侧开始。

3、count():返回某个⼦子串串在字符串串中出现的次数

语法:字符串串序列列.count(⼦子串串, 开始位置下标, 结束位置下标)

mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1

修改

1、replace():替换

语法:字符串序列.replace(旧⼦串, 新⼦串, 替换次数)

mystr = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(mystr)

字符串类型的数据修改的时候不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。

2、split():按照指定字符分割字符串。

语法:字符串序列.split(分割字符, num)

num表示的是分割字符出现的次数,即将来返回数据个数为num+1个。

mystr = "hello world and itcast and itheima and Python"
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))

3、join():用一个字符或⼦串合并字符串,即是将多个字符串合并为个新的字符串。

语法:字符或⼦串.join(多字符串组成的序列)

list1 = ['chuan', 'zhi', 'bo', 'ke']

# 结果:chuan_zhi_bo_ke
print('_'.join(list1))

4、字符转换

  • capitalize():将字符串第一个字符转换成大写
  • title():将字符串每个单词首字母转换成大写
  • lower():将字符串中大写转小写
  • upper():将字符串中⼩写转大写
mystr = "hello world and itcast and itheima and Python"

# 结果:Hello world and itcast and itheima and python
print(mystr.capitalize())

# 结果:Hello World And Itcast And Itheima And Python
print(mystr.title())

# 结果:hello world and itcast and itheima and python
print(mystr.lower())

# 结果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())
  • lstrip():删除字符串左侧空白字符
  • rstrip():删除字符串右侧空⽩字符
  • strip():删除字符串两侧空白字符


在这里插入图片描述

  • ljust():返回一个原字符串左对齐,并使用指定字符(默认空格)填充⾄至对应⻓度的新字符串。

语法:字符串序列.ljust(⻓度, 填充字符)

  • rjust():返回⼀个原字符串右对⻬,并使⽤指定字符(默认空格)填充⾄至对应⻓度的新字符串,语法和ljust()相同。
  • center():返回⼀个原字符串居中对齐,并使⽤指定字符(默认空格)填充⾄对应长度的新字符串,语法和ljust()相同。


在这里插入图片描述

判断

  • startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
mystr = "hello world and itcast and itheima and Python "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))
  • endswith():检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开始和结束位置下标,则在指定范围内检查。
mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))
  • isalpha():如果字符串至少有⼀个字符并且所有字符都是字母则返回 True, 否则返回 False。
  • isdigit():如果字符串只包含数字则返回 True 否则返回 False。
  • isalnum():如果字符串至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回False。
  • isspace():如果字符串中只包含空白,则返回 True,否则返回False。
mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())
-----------------------------
mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())
-----------------------------
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())
-----------------------------
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python if else条件语句形式详解

    Python if else条件语句形式详解

    这篇文章主要介绍了Python if else条件语句形式详解,在 Python 中,可以使用 if else 语句对条件进行判断,然后根据不同的结果执行不同的代码,这称为选择结构或者分支结构,接下来小编就根据情况的不同介绍if else条件语句形式的不同,需要的朋友可以参考一下
    2022-03-03
  • Python中Scrapy爬虫图片处理详解

    Python中Scrapy爬虫图片处理详解

    这篇文章主要介绍了Python中Scrapy爬虫图片处理方式和原理,需要的朋友学习参考下吧。
    2017-11-11
  • Python中if __name__==‘__main__‘用法详情

    Python中if __name__==‘__main__‘用法详情

    这篇文章主要介绍了Python中if __name__==‘__main__‘用法详情,文章首先通过我们先定义一个test01.py的文件展开详情,具有一定的参考价值,感兴趣的朋友可以参考一下
    2022-06-06
  • 使用numpy对数组求平均时如何忽略nan值

    使用numpy对数组求平均时如何忽略nan值

    这篇文章主要介绍了使用numpy对数组求平均时如何忽略nan值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • python调用自定义函数的实例操作

    python调用自定义函数的实例操作

    在本文里我们给大家整理了关于python调用自定义函数的实例操作相关内容,有此需要的朋友们可以学习参考下。
    2019-06-06
  • pandas删除部分数据后重新生成索引的实现

    pandas删除部分数据后重新生成索引的实现

    这篇文章主要介绍了pandas删除部分数据后重新生成索引的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • pycharm远程linux开发和调试代码的方法

    pycharm远程linux开发和调试代码的方法

    这篇文章主要介绍了pycharm远程linux开发和调试代码的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • python脚本实现查找webshell的方法

    python脚本实现查找webshell的方法

    这篇文章主要介绍了python脚本实现查找webshell的方法,是很实用的一个功能,需要的朋友可以参考下
    2014-07-07
  • Python采集王者皮肤图片实战示例

    Python采集王者皮肤图片实战示例

    这篇文章主要为大家介绍了Python采集王者皮肤图片实战示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Python字典“键”和“值”的排序5种方法

    Python字典“键”和“值”的排序5种方法

    这篇文章主要介绍了5种Python字典“键”和“值”的排序方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03

最新评论