網站首頁 編程語言 正文
刪除字符串最后一個字符的方法
1.使用strip()方法刪除最后一個字符
Python strip() 方法用于移除字符串頭尾指定的字符(默認為空格或換行符)或字符序列。
- strip()方法語法:str.strip([chars]);
- 參數(shù):chars – 移除字符串頭尾指定的字符序列。
示例:
str = "0000000this is string example....wow!!!0000000";
print str.strip( '0' );
輸出結果:
this is string example....wow!!!
2.將字符串轉換為列表
然后使用pop()方法刪除最后一個字符
a = 'abcd efg'
b = list(a)?
b.pop()
print(b)
輸出結果如下:
['a', 'b', 'c', 'd', ' ', 'e', 'f']
去除字符串(去除前面幾個或者是后面幾個)
a = "16541616584984"
a = a[2:-2]
print(a)
輸出結果:
5416165849
a[2:-2] 表示去掉前面兩個和后面兩個
如果光去掉后面的a[:-2]
去掉前面的操作以此類推。。。。
原文鏈接:https://www.py.cn/faq/python/17942.html
相關推薦
- 2022-04-28 oracle重置序列從0開始遞增1_oracle
- 2022-05-25 pytorch?hook?鉤子函數(shù)的用法_python
- 2022-07-23 C++深入細致探究二叉搜索樹_C 語言
- 2022-07-25 pandas實現(xiàn)數(shù)據(jù)讀取&清洗&分析的項目實踐_python
- 2022-09-19 Tomcat10配置端口號為443(使用https訪問)_Tomcat
- 2022-03-15 When allowCredentials is true, allowedOrigins cann
- 2022-01-06 解決 el-form 異步校驗導致重復校驗的問題
- 2023-01-11 C++入門教程之引用與指針_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支