網站首頁 編程語言 正文
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
相關推薦
- 2022-05-18 解決iOS驗證碼顯示在左邊問題_IOS
- 2023-07-08 el-radio-group選中后再次點擊取消,及el-radio-button具有邊框懸浮問題解決
- 2022-05-18 Python?pandas?計算每行的增長率與累計增長率_python
- 2022-05-20 Redis 做接口限流,一個注解的事
- 2022-11-21 關于Fragment?already?added問題的解決方案_Android
- 2024-04-08 MAC更新和使用composer
- 2022-01-11 for循環調用接口返回的數據放在同一個列表中
- 2022-10-15 Go?Excelize?API源碼解讀GetSheetViewOptions與SetPageLayo
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支