python如何尋找主串中所有指定子串下標
python尋找主串中所有指定子串下標
該函數(shù)可實現(xiàn)顯示字符串中指定子串所有下標(首字下標)
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)“天安門”的下標,8表示第二次出現(xiàn)的下標。(由0開始)
python字符串常用操作
查找
1、find():檢測某個?串是否包含在這個字符串中,如果在,返回這個串開始的位置下標,否則則返回-1。
語法:字符串串序列列.find(?子串串, 開始位置下標, 結(jié)束位置下標)
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(?子串串, 開始位置下標, 結(jié)束位置下標)
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()功能相同,但查找?方向為右側(cè)開始。rindex():和index()功能相同,但查找?方向為右側(cè)開始。
3、count():返回某個?子串串在字符串串中出現(xiàn)的次數(shù)
語法:字符串串序列列.count(?子串串, 開始位置下標, 結(jié)束位置下標)
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ù)據(jù)的類型即是不可變類型。
2、split():按照指定字符分割字符串。
語法:字符串序列.split(分割字符, num)
num表示的是分割字符出現(xiàn)的次數(shù),即將來返回數(shù)據(jù)個數(shù)為num+1個。
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():用一個字符或?串合并字符串,即是將多個字符串合并為個新的字符串。
語法:字符或?串.join(多字符串組成的序列)
list1 = ['chuan', 'zhi', 'bo', 'ke']
# 結(jié)果:chuan_zhi_bo_ke
print('_'.join(list1))
4、字符轉(zhuǎn)換
capitalize():將字符串第一個字符轉(zhuǎn)換成大寫title():將字符串每個單詞首字母轉(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():返回一個原字符串左對齊,并使用指定字符(默認空格)填充?至對應?度的新字符串。
語法:字符串序列.ljust(?度, 填充字符)
rjust():返回?個原字符串右對?,并使?指定字符(默認空格)填充?至對應?度的新字符串,語法和ljust()相同。center():返回?個原字符串居中對齊,并使?指定字符(默認空格)填充?對應長度的新字符串,語法和ljust()相同。

判斷
startswith():檢查字符串是否是以指定?串開頭,是則返回 True,否則返回 False。如果設置開始和結(jié)束位置下標,則在指定范圍內(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。如果設置開始和結(jié)束位置下標,則在指定范圍內(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():如果字符串至少有?個字符并且所有字符都是字母則返回 True, 否則返回 False。isdigit():如果字符串只包含數(shù)字則返回 True 否則返回 False。isalnum():如果字符串至少有一個字符并且所有字符都是字母或數(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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python GUI庫圖形界面開發(fā)之PyQt5打開保存對話框QFileDialog詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5打開保存對話框QFileDialog詳細使用方法與實例,需要的朋友可以參考下2020-02-02
python?HTTP協(xié)議相關庫requests urllib基礎學習
這篇文章主要介紹了python?HTTP協(xié)議相關庫requests urllib基礎學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
python執(zhí)行shell并獲取結(jié)果的詳細示例
在Python中執(zhí)行Shell命令并獲取其結(jié)果,通常可以使用subprocess模塊,這個模塊允許我們啟動新的進程,連接到它們的輸入/輸出/錯誤管道,并獲取它們的返回碼,下面是一個詳細的示例,展示了如何使用subprocess.run()函數(shù)來執(zhí)行Shell命令并獲取其輸出,需要的朋友可以參考下2024-07-07
讓Django支持Sql Server作后端數(shù)據(jù)庫的方法
今天小編就為大家分享一篇讓Django支持Sql Server作后端數(shù)據(jù)庫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python實戰(zhàn)項目之MySQL tkinter pyinstaller實現(xiàn)學生管理系統(tǒng)
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用MySQL、tkinter、 pyinstaller實現(xiàn)一個學生管理系統(tǒng),大家可以通過案例查缺補漏,提升水平2021-10-10
使用django-crontab實現(xiàn)定時任務的示例
這篇文章主要介紹了使用django-crontab實現(xiàn)定時任務,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

