python如何寻找主串中所有指定子串下标
作者:头秃头凉凉 发布时间:2023-11-27 08:38:41
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())
来源:https://blog.csdn.net/weixin_44717777/article/details/123579721


猜你喜欢
- 下面我挑选出的这几个技巧常常会被人们忽略,但它们在日常编程中能真正的给我们带来不少帮助。1. 字典推导(Dictionary compreh
- 本文实例讲述了Go语言模拟while语句实现无限循环的方法。分享给大家供大家参考。具体实现方法如下:这段代码把for语句当成C语言里的whi
- 一、排序的基本概念和分类所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。排序算法,就是如何使得记录按
- 我们有时请求服务器时,无论get或post请求,会出现403错误,这是因为服务器拒绝了你的访问,这时我们可以通过模拟浏览器的头部信息进行访问
- php 如何获取请求的xml数据,对方通过http协议post提交过来xml数据,php如何获取到这些数据呢?<?php $xml_d
- 如果你已经有使用JavaScript的经验,你可能已经知道这两个看似相同的方法:Array.prototype.map()和Array.pr
- 一、概念1、模块化代码可以使代码易于维护和调试,并且提高代码的重用性;2、函数可以用来减少冗余的代码并提高代码的可重用性。函数也可以用来模块
- 用pandas中的DataFrame时选取行或列:import numpy as npimport pandas as pdfrom pan
- 题目[1]:格式输出练习。在交互式状态下完成以下练习。运行结果截图:题目[2]:格式输出练习。在.py的文件中完成以下练习代码:num =
- 什么是deferdefer用来声明一个延迟函数,把这个函数放入到一个栈上, 当外部的包含方法return之前,返回参数到调用方法之前调用,也
- SqlServer帮助中对扩展属性的描述是: The Extended Properties property sets or retrie
- 1、判断多个条件的语句,if为真则执行if后面的语句。2、如果elif是真的,则执行elif,后面的代码块不执行。3、如果if和elif不满
- 对于小型站点,使用七牛云存储的免费配额已足够为站点提供稳定、快速的存储服务七牛云存储已有Python SDK,对它进行简单封装后,就可以直接
- 我们可用正规表达式来寻找并替换URL和邮件地址为活动的超级链接。用到的主要函数就是InsertHyperlinks(inText),语法为:
- 一、 yaml1、 准备支持的数据类型:字典、列表、字符串、布尔值、整数、浮点数、Null、时间等基本语法规则:大小写敏感使用缩进表示层级关
- 代码片段是开发者每天都要面对的东西,甚至有时候查找代码片段的时间比编写新代码的时间还要多。因为如果找到能够在项目中直接使用的代码片段,这意味
- 本文实例讲述了python实现读取excel文件中所有sheet操作。分享给大家供大家参考,具体如下:表格是这样的 实现把此文件所
- 本文实例为大家分享了python实现杨氏矩阵查找的具体代码,供大家参考,具体内容如下问题描述:在一个m行n列二维数组中,每一行都按照从左到右
- 目录1、封装1.1 私有属性和私有方法2、继承2.1 方法的重写2.2 在子类方法中调用父类方法2.3 多继承2.4 新式类和经典类3、多态
- Go素数筛选分析1. 素数筛选介绍学习Go语言的过程中,遇到素数筛选的问题。这是一个经典的并发编程问题,是某大佬的代码,短短几行代码就实现了