網站首頁 編程語言 正文
1:訪str單個字符
#for循環迭代 name = 'Chengwei' for ch in name: print(ch, end=' ') #C h e n g w e i #索引 print(name[1]) #h print(name[-1]) #最后一個為 -1 #i #len()函數返回str字符串數量 print(len(name)) #8
2: 字符串連接
message = 'hello' + 'world' print(message) #helloworld
3:str切片
message = 'helloworld' print(message[2:4]) #ll
4:使用in 和not in 測試字符串
message = 'helloworld' if 'hello' in message: print('YES') #YES if 'aaa' not in message: print('YES') #YES
5:str方法
字符串測試方法
isalnum() | 如果str只包含字母或數字,并且長度至少為一個字符,則返回true。否則返回false |
isalpha() | 如果str只包含字母并且長度至少為一個字符,則返回true。否則返回false |
isdigit() | 如果str只包含數字如果str只包含字母并且長度至少為一個字符,則返回true。否則返回false |
islower() | 如果str中的所有字母都是小寫如果str只包含數字如果str只包含字母并且長度至少為一個字符,則返回true。否則返回false |
isspace() | 如果str只包含空白字符,并且長度至少為一個字符,則返回true。否則返回false(空格,\n,\t) |
isupper() | 如果str中的所有字母都是大寫如果str只包含數字如果str只包含字母并且長度至少為一個字符,則返回true。否則返回false |
字符串修改方法
lower() | 返回將所有字母轉換為小寫的str副本。不是字母的不變 |
lstrip() | 返回刪除所有前導空白字符的str副本。 |
lstrip(str_item) | str_item是字符串。返回刪除所有前導str_item的字符串副本 |
rstrip() | 返回所有尾部空白字符串的字符串副本。 |
rstrip(str_item) | 返回刪除所有尾部str_item的字符串副本 |
strip() | ? |
strip(str_item) | 刪除所有前導和尾部str_item的字符串副本 |
upper() | ? |
lower() | 返回將所有字母轉換為小寫的str副本。不是字母的不變 |
lstrip() | 返回刪除所有前導空白字符的str副本。 |
lstrip(str_item) | str_item是字符串。返回刪除所有前導str_item的字符串副本 |
rstrip() | 返回所有尾部空白字符串的字符串副本。 |
rstrip(str_item) | 返回刪除所有尾部str_item的字符串副本 |
strip() | ? |
strip(str_item) | 刪除所有前導和尾部str_item的字符串副本 |
upper() | ? |
搜索和替換的方法
endswith(substring) | 返回true或false |
find(substring) | 返回找到substring的最小索引位置。沒有找到返回 -1 |
replace(old, new) | 返回將所有的old替換為new的字符串副本 |
starstwith(substring) | 返回true或false |
6:重復操作符
print('hello' * 2) #hellohello
7:分割字符串
message = 'hello world chengwei' message_list = message.split() print(message_list) #['hello', 'world', 'chengwei'] message = 'hello,world,chengwei' message_list = message.split(',') print(message_list) #['hello', 'world', 'chengwei']
總結
原文鏈接:https://blog.csdn.net/cw_hstx/article/details/122517790
相關推薦
- 2023-04-18 python去除字符串中的空格、特殊字符和指定字符的三種方法_python
- 2022-07-04 python生成單位陣或對角陣的三種方式小結_python
- 2023-04-07 C++日期類運算符重載方式_C 語言
- 2022-06-06 Lombok報錯:You aren‘t using a compiler supported by
- 2022-04-10 Python?tkinter實現計算器功能_python
- 2023-07-30 ES6 module語法中export和import的使用
- 2022-08-27 C++?Thread實現簡單的socket多線程通信_C 語言
- 2022-06-18 android實現可拖動的浮動view_Android
- 最近更新
-
- 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同步修改后的遠程分支