網站首頁 編程語言 正文
python 字符串切割 maxsplit
my_str.split(str1, maxsplit)
str1 可以不寫,默認是空白字符(" " “\t” “\n”)
將my_str 這個字符串按照str1 進行切割, maxsplit 割幾次
my_str = "hello world itcast and itcastcpp"
my_str1 = my_str.split(" ")
print(my_str1)
my_str2 = my_str.split(" ", 1)
print(my_str2)
my_str3 = my_str.split() # 用的最多
print(my_str3)
my_str4 = my_str.split("itcast")
print(my_str4)
# 輸出結果是
['hello', 'world', 'itcast', 'and', 'itcastcpp']
['hello', 'world itcast and itcastcpp']
['hello', 'world', 'itcast', 'and', 'itcastcpp']
['hello world ', ' and ', 'cpp']
python字符串切割split和rsplit函數
1. split(sep, maxsplit)
切分字符串,返回切分后的列表
sep,分隔符,默認空格
maxsplit,切分次數,默認最大次數,從起始位置開始計數
示例1:默認
s = 'a b c'
res = s.split()
res
['a', 'b', 'c']
示例2:指定參數
s = 'a b c'
res = s.split(sep=' ', maxsplit=1)
res
['a', 'b c']
示例3:位置參數
s = 'a.b.c'
res = s.split('.', 1)
res
['a', 'b.c']
2. rsplit(sep, maxsplit)
類似split,區別為從結尾位置開始計數
sep,分隔符,默認空格
maxsplit,切分次數,默認最大次數,從起始結尾開始計數
示例1:默認
s = 'a b c'
res = s.rsplit()
res
['a', 'b', 'c']
示例2:指定參數
s = 'a b c'
res = s.rsplit(sep=' ', maxsplit=1)
res
['a b', 'c']
示例3:位置參數
s = 'a.b.c'
res = s.rsplit('.', 1)
res
['a.b', 'c']
總結
原文鏈接:https://blog.csdn.net/LanlanDeming/article/details/103318399
相關推薦
- 2022-06-27 Abp集成HangFire開源.NET任務調度框架_實用技巧
- 2022-11-11 C語言qsort函數使用方法詳解_C 語言
- 2022-11-18 Redux模塊化拆分reducer函數流程介紹_React
- 2022-09-27 使用Python?matplotlib繪制簡單的柱形圖、折線圖和直線圖_python
- 2023-04-27 react?render的原理及觸發時機說明_React
- 2021-12-13 淺談Gin框架中bind的使用_Golang
- 2023-10-14 SQLServer 發送HTTP請求
- 2022-05-22 nginx常用配置conf的示例代碼詳解_nginx
- 最近更新
-
- 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同步修改后的遠程分支