網站首頁 編程語言 正文
index()
str = "hello world"
print(str.index("l",5,11)) #結果:9
print(str.index("l",3)) # 結果為:3
print(str.index("l")) #結果為:2
print(str.index("c")) #結果: 報錯!??!
語法:變量.index(指定字符,開始下標,結束下標)
開始下標(即 5)與結束下標(即 11)可以不寫,默認為從頭到尾查找。
有開始下標與結束下標時查找的是這個開始于結束的字符串區間。
當在字符串(即 str)中查找到指定字符(即 l)時返回指定字符在字符串中的下標位置。
如果沒在字符串中查找到指定字符時會報錯??!
substring not found
rindex()
str = "hello world"
print(str.rindex("l",5,11)) #結果:9
print(str.rindex("l",3)) # 結果為:9
print(str.rindex("l")) #結果為:9
print(str.rindex("c")) #結果: 報錯!?。?/code>
語法:變量.rindex(指定字符,開始下標,結束下標)
從右邊開始查找指定字符(即 l)返回指定字符在字符串(即 str)中的下標。
開始下標(即 5)與結束下標(即 11)可以不寫,默認查找整個字符串。
有開始下標與結束下標時查找的是這個開始于結束的字符串區間。
當在字符串(即 str)中查找到指定字符(即 l)時返回指定字符在字符串中的下標位置。
如果沒在字符串中查找到指定字符時會報錯??!
substring not found
知識點擴展:
用Python操作字符串之rindex()方法的使用
想了解用Python操作字符串之rindex()方法的使用的相關內容嗎,在本文為您仔細講解Python的rindex()方法的相關知識和一些Code實例,歡迎閱讀和指正,我們先劃重點:Python,下面大家一起來學習吧。
?rindex()方法返回所在的子str被找到的最后一個索引,可選擇限制搜索的字符串string[beg:end] 如果沒有這樣的索引存在,拋出一個異常。
語法
以下是rindex()方法的語法:
str.rindex(str, beg=0 end=len(string))
參數
- ??? str -- 此選項指定要搜索的字符串。
- ??? beg -- 這是開始索引,默認情況下為 0
- ??? len -- 這是最后的索引,默認情況下它等于字符串的長度。
返回值
此方法如果找到返回最后一個索引,否則如果沒有找到str引發一個異常。
例子
下面的例子顯示了rindex()方法的使用。
#!/usr/bin/python
str1 = "this is string example....wow!!!";
str2 = "is";
print str1.rindex(str2);
print str1.index(str2);
當我們運行上面的程序,它會產生以下結果:
5
2
原文鏈接:https://blog.csdn.net/weifei20001217/article/details/128407202
相關推薦
- 2022-04-18 pytorch自定義loss損失函數_python
- 2024-01-08 Spring AOP 切面@Around注解的具體使用
- 2022-11-13 如何修改npm默認源為淘寶源
- 2022-10-19 報錯No?module?named?numpy問題的解決辦法_python
- 2023-01-14 C#實現啟動項管理的示例代碼_C#教程
- 2023-11-13 matplotlib按照論文要求繪圖并保存pdf格式
- 2023-08-16 uniapp插件uni-combox實現失去焦點驗證數據是否存在
- 2023-07-09 【elementplus】解決el-table開啟show-overflow-tooltip后,to
- 最近更新
-
- 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同步修改后的遠程分支