python如何尋找主串中所有指定子串下標(biāo)
python尋找主串中所有指定子串下標(biāo)
該函數(shù)可實(shí)現(xiàn)顯示字符串中指定子串所有下標(biāo)(首字下標(biāo))
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 = "我們?nèi)チ颂彀查T,天安門附近有很多人" list = subStrIndex('天安門',str) print(list)
輸出結(jié)果:[4,8]
其中4表示第一次出現(xiàn)“天安門”的下標(biāo),8表示第二次出現(xiàn)的下標(biāo)。(由0開始)
python字符串常用操作
查找
1、find():檢測某個(gè)?串是否包含在這個(gè)字符串中,如果在,返回這個(gè)串開始的位置下標(biāo),否則則返回-1。
語法:字符串串序列列.find(?子串串, 開始位置下標(biāo), 結(jié)束位置下標(biāo))
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():檢測某個(gè)?串是否包含在這個(gè)字符串中,如果在返回這個(gè)子串開始的位置下標(biāo),否則報(bào)異常。
語法:字符串串序列列.index(?子串串, 開始位置下標(biāo), 結(jié)束位置下標(biāo))
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')) # 報(bào)錯(cuò)
rfind()
: 和find()功能相同,但查找?方向?yàn)橛覀?cè)開始。rindex()
:和index()功能相同,但查找?方向?yàn)橛覀?cè)開始。
3、count():返回某個(gè)?子串串在字符串串中出現(xiàn)的次數(shù)
語法:字符串串序列列.count(?子串串, 開始位置下標(biāo), 結(jié)束位置下標(biāo))
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(舊?串, 新?串, 替換次數(shù))
mystr = "hello world and itcast and itheima and Python" # 結(jié)果:hello world he itcast he itheima he Python print(mystr.replace('and', 'he')) # 結(jié)果:hello world he itcast he itheima he Python print(mystr.replace('and', 'he', 10)) # 結(jié)果:hello world and itcast and itheima and Python print(mystr)
字符串類型的數(shù)據(jù)修改的時(shí)候不能改變?cè)凶址?,屬于不能直接修改?shù)據(jù)的類型即是不可變類型。
2、split():按照指定字符分割字符串。
語法:字符串序列.split(分割字符, num)
num表示的是分割字符出現(xiàn)的次數(shù),即將來返回?cái)?shù)據(jù)個(gè)數(shù)為num+1個(gè)。
mystr = "hello world and itcast and itheima and Python" # 結(jié)果:['hello world ', ' itcast ', ' itheima ', ' Python'] print(mystr.split('and')) # 結(jié)果:['hello world ', ' itcast ', ' itheima and Python'] print(mystr.split('and', 2))
3、join():用一個(gè)字符或?串合并字符串,即是將多個(gè)字符串合并為個(gè)新的字符串。
語法:字符或?串.join(多字符串組成的序列)
list1 = ['chuan', 'zhi', 'bo', 'ke'] # 結(jié)果:chuan_zhi_bo_ke print('_'.join(list1))
4、字符轉(zhuǎn)換
capitalize()
:將字符串第一個(gè)字符轉(zhuǎn)換成大寫title()
:將字符串每個(gè)單詞首字母轉(zhuǎn)換成大寫lower()
:將字符串中大寫轉(zhuǎn)小寫upper()
:將字符串中?寫轉(zhuǎn)大寫
mystr = "hello world and itcast and itheima and Python" # 結(jié)果:Hello world and itcast and itheima and python print(mystr.capitalize()) # 結(jié)果:Hello World And Itcast And Itheima And Python print(mystr.title()) # 結(jié)果:hello world and itcast and itheima and python print(mystr.lower()) # 結(jié)果:HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON print(mystr.upper())
lstrip()
:刪除字符串左側(cè)空白字符rstrip()
:刪除字符串右側(cè)空?字符strip()
:刪除字符串兩側(cè)空白字符
ljust()
:返回一個(gè)原字符串左對(duì)齊,并使用指定字符(默認(rèn)空格)填充?至對(duì)應(yīng)?度的新字符串。
語法:字符串序列.ljust(?度, 填充字符)
rjust()
:返回?個(gè)原字符串右對(duì)?,并使?指定字符(默認(rèn)空格)填充?至對(duì)應(yīng)?度的新字符串,語法和ljust()相同。center()
:返回?個(gè)原字符串居中對(duì)齊,并使?指定字符(默認(rèn)空格)填充?對(duì)應(yīng)長度的新字符串,語法和ljust()相同。
判斷
startswith()
:檢查字符串是否是以指定?串開頭,是則返回 True,否則返回 False。如果設(shè)置開始和結(jié)束位置下標(biāo),則在指定范圍內(nèi)檢查。
mystr = "hello world and itcast and itheima and Python " # 結(jié)果:True print(mystr.startswith('hello')) # 結(jié)果False print(mystr.startswith('hello', 5, 20))
endswith()
:檢查字符串是否是以指定?串結(jié)尾,是則返回 True,否則返回 False。如果設(shè)置開始和結(jié)束位置下標(biāo),則在指定范圍內(nèi)檢查。
mystr = "hello world and itcast and itheima and Python" # 結(jié)果:True print(mystr.endswith('Python')) # 結(jié)果:False print(mystr.endswith('python')) # 結(jié)果:False print(mystr.endswith('Python', 2, 20))
isalpha()
:如果字符串至少有?個(gè)字符并且所有字符都是字母則返回 True, 否則返回 False。isdigit()
:如果字符串只包含數(shù)字則返回 True 否則返回 False。isalnum()
:如果字符串至少有一個(gè)字符并且所有字符都是字母或數(shù)字則返回 True,否則返回False。isspace()
:如果字符串中只包含空白,則返回 True,否則返回False。
mystr1 = 'hello' mystr2 = 'hello12345' # 結(jié)果:True print(mystr1.isalpha()) # 結(jié)果:False print(mystr2.isalpha()) ----------------------------- mystr1 = 'aaa12345' mystr2 = '12345' # 結(jié)果: False print(mystr1.isdigit()) # 結(jié)果:False print(mystr2.isdigit()) ----------------------------- mystr1 = 'aaa12345' mystr2 = '12345-' # 結(jié)果:True print(mystr1.isalnum()) # 結(jié)果:False print(mystr2.isalnum()) ----------------------------- mystr1 = '1 2 3 4 5' mystr2 = ' ' # 結(jié)果:False print(mystr1.isspace()) # 結(jié)果:True print(mystr2.isspace())
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python之pymysql模塊簡單應(yīng)用示例代碼
這篇文章主要介紹了python之pymysql模塊簡單應(yīng)用示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12PyQt5 designer 頁面點(diǎn)擊按鈕跳轉(zhuǎn)頁面實(shí)現(xiàn)
本文主要介紹了PyQt5 designer 頁面點(diǎn)擊按鈕跳轉(zhuǎn)頁面實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Python多進(jìn)程通信Queue、Pipe、Value、Array實(shí)例
這篇文章主要介紹了Python多進(jìn)程通信Queue、Pipe、Value、Array實(shí)例,queue和pipe用來在進(jìn)程間傳遞消息、Value + Array 是python中共享內(nèi)存映射文件的方法,需要的朋友可以參考下2014-11-11python中協(xié)程實(shí)現(xiàn)TCP連接的實(shí)例分析
在本篇文章中我們給大家分享了python中協(xié)程實(shí)現(xiàn)TCP連接的代碼示例內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)下。2018-10-10對(duì)Python中的@classmethod用法詳解
下面小編就為大家分享一篇對(duì)Python中的@classmethod用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04如何利用Python實(shí)現(xiàn)簡易的音頻播放器
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)簡易的音頻播放器,需要用到的庫有pygame和tkinter,實(shí)現(xiàn)音頻播放的功能,供大家學(xué)習(xí)參考,希望對(duì)你有所幫助2022-03-03jenkins配置python腳本定時(shí)任務(wù)過程圖解
這篇文章主要介紹了jekins配置python腳本定時(shí)任務(wù)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例
這篇文章主要介紹了python+requests+pytest接口自動(dòng)化的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Python Reduce函數(shù)的高級(jí)用法詳解
這篇文章主要介紹了reduce函數(shù)的工作原理和應(yīng)用,同時(shí)提供豐富的示例代碼,方便更好地理解如何使用reduce函數(shù)來輕松解決復(fù)雜的數(shù)據(jù)聚合問題,需要的可以參考下2023-11-11