網站首頁 編程語言 正文
1、字符串的索引與獲取
字符串的索引方式與列表的索引方式是一樣的。只不過列表是每個元素的自身就有一個索引位置,而字符串是每個字符就有一個索引位置。
- 索引規則與列表相同
- 切片和索引的獲取與列表相同
- 無法通過索引進行修改和刪除操作(字符串不可修改)
示例如下:
name = 'Adem' print(name[0]) print(name[-1])
?執行結果如下:
?>>> A
>>> m
2、字符串的 find 與 index 函數
find 與 index 函數的功能:獲取元素的索引位置
find 與 index 函數的用法:
- string.index(item) —> item:查詢個數的元素,返回索引位置
- string.find(item) —> item:查詢個數的元素,返回索引位置
find 與 index 函數的區別:
- find 如果獲取不到,返回 -1
- index 如果獲取不到,則直接報錯
示例如下:
info = 'My name is Neo' print(info.find('Neo')) print(info.index('Neo'))
執行結果如下:
>>> 11
?>>> 11
info = 'My name is Neo' print(info.find('Jack')) print(info.index('Jack'))
執行結果如下:
>>> -1
>>> ValueError: substring not found
原文鏈接:https://blog.csdn.net/weixin_42250835/article/details/123153592
相關推薦
- 2022-09-22 使用 sed 替換字符串中最后一次出現的字符
- 2022-01-31 jq監聽input type="file"發生改變,即選擇文件,并獲取文件名稱
- 2022-09-18 iOS開發技能weak和strong修飾符的規范使用詳解_IOS
- 2023-04-08 react中實現拖拽排序react-dnd功能_React
- 2022-08-21 使用?DataAnt?監控?Apache?APISIX的原理解析_Linux
- 2023-12-19 spring boot configuration annotation processor not
- 2022-02-11 idea package合在一起,利用Compact Middle Packages解決 &
- 2022-06-17 C語言詳解函數與指針的使用_C 語言
- 最近更新
-
- 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同步修改后的遠程分支