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实现对比Exce的工具

    基于Python实现对比Exce的工具

    这篇文章主要介绍了基于Python实现对比Excel的小工具,通过循环对比组合列(主键+对比列)结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2022-04-04
  • python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例

    python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例

    这篇文章主要介绍了python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例,需要的朋友可以参考下
    2020-02-02
  • python HTTP协议相关库requests urllib基础学习

    python HTTP协议相关库requests urllib基础学习

    这篇文章主要介绍了python HTTP协议相关库requests urllib基础学习,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • 将python程序打包成DLL的三种方式

    将python程序打包成DLL的三种方式

    这篇文章主要介绍了将python程序打包成DLL的三种方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • Python中作用域的规则与应用

    Python中作用域的规则与应用

    在 Python 编程中,作用域(Scope) 是指一个变量可以被访问和引用的范围,这篇文章主要为大家介绍了Python中作用域的规则与应用的相关知识,需要的可以了解下
    2025-01-01
  • python执行shell并获取结果的详细示例

    python执行shell并获取结果的详细示例

    在Python中执行Shell命令并获取其结果,通常可以使用subprocess模块,这个模块允许我们启动新的进程,连接到它们的输入/输出/错误管道,并获取它们的返回码,下面是一个详细的示例,展示了如何使用subprocess.run()函数来执行Shell命令并获取其输出,需要的朋友可以参考下
    2024-07-07
  • 让Django支持Sql Server作后端数据库的方法

    让Django支持Sql Server作后端数据库的方法

    今天小编就为大家分享一篇让Django支持Sql Server作后端数据库的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • Python实战项目之MySQL tkinter pyinstaller实现学生管理系统

    Python实战项目之MySQL tkinter pyinstaller实现学生管理系统

    读万卷书不如行万里路,只学书上的理论是远远不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用MySQL、tkinter、 pyinstaller实现一个学生管理系统,大家可以通过案例查缺补漏,提升水平
    2021-10-10
  • Python中使用matplotlib库绘制各种图

    Python中使用matplotlib库绘制各种图

    这篇文章主要介绍了Python中使用matplotlib库绘制各种图方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 使用django-crontab实现定时任务的示例

    使用django-crontab实现定时任务的示例

    这篇文章主要介绍了使用django-crontab实现定时任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02

最新评论