網(wǎng)站首頁 編程語言 正文
python基礎(chǔ)之字符串常用方法??????
持續(xù)更新python基礎(chǔ)知識(shí),歡迎各位來訪~??????
字符串是什么?
字符串就是一系列字符。字符串屬于不可變序列,在python中,用引號(hào)包裹的都是字符串,其中引號(hào)可以是單引號(hào),雙引號(hào),也可以是三引號(hào)(單,雙引號(hào)中的字符必須在一行,三引號(hào)中的字符可以分布在多行)
txt = 'hello world' # 使用單引號(hào),字符串內(nèi)容必須在一行
txt1 = "hello python world " # 使用雙引號(hào),字符串內(nèi)容必須在一行
# 使用三引號(hào),字符串內(nèi)容可以分布在多行
txt2 = '''life is short
i use python '''
字符串常用方法
1.find()
定義 find()方法返回該元素最小索引值(找不到返回-1) ??舉個(gè)栗子??返回"python"的最小索引值
txt = "hello python world." res = txt.find("python") print(res)
運(yùn)行結(jié)果如下:
6
2.index()
定義 index()方法返回該元素最小索引值(找不到元素會(huì)報(bào)錯(cuò)) ??舉個(gè)栗子??返回"world"的最小索引值
txt = "hello python world." res = txt.index("world") print(res)
運(yùn)行結(jié)果如下:
13
3.startswith()
定義 startswith() 方法如果字符串以指定值開頭,返回True,否則返回False ??舉個(gè)栗子??判斷字符串是不是以"hello"開頭
txt = "hello python world." res = txt.startswith("hello") print(res)
運(yùn)行結(jié)果如下:
True
4.endswith()
定義 endswith() 方法如果字符串以指定值結(jié)束,返回True,否則返回False ??舉個(gè)栗子??判斷字符串是不是以"hello"結(jié)束
txt = "hello python world." res = txt.endswith("hello") print(res)
運(yùn)行結(jié)果如下:
Flase
5.count()
定義 count() 方法返回指定值在字符串中出現(xiàn)的次數(shù)。 ??舉個(gè)栗子??統(tǒng)計(jì)"o"出現(xiàn)次數(shù)
txt = "hello python world." res = txt.count("o") print(res)
運(yùn)行結(jié)果如下:
3
6.join()
定義 join() 方法獲取可迭代對(duì)象中的所有項(xiàng)目,并將它們連接為一個(gè)字符串。必須將字符串指定為分隔符 ??舉個(gè)栗子??使用"-"作為分割符,將列表中的所有項(xiàng)連接到字符串中
res = ['h','e','l','l','o'] print('-'.join(res))
運(yùn)行結(jié)果如下:
h-e-l-l-o
7.upper()
定義 upper()方法將字符串全部轉(zhuǎn)為大寫 ??舉個(gè)栗子??將字符串"hello python world"全部轉(zhuǎn)為大寫
tet = "hello python world" res = txt.upper() print(res)
運(yùn)行結(jié)果如下:
HELLO WORLD
8.lower()
定義 lower()方法將字符串全部轉(zhuǎn)為小寫 ??舉個(gè)栗子??將字符串"HELLO PYTHON WORLD"全部轉(zhuǎn)為小寫
tet = "HELLO PYTHON WORLD" res = txt.lower() print(res)
運(yùn)行結(jié)果如下:
hello python world
9.split()
定義 split()方法以指定字符分割字符串,并返回列表 ??舉個(gè)栗子??以?號(hào)作為分隔符,分割字符串
txt = "hello?python?world" res = txt.split("?") print(res)
運(yùn)行結(jié)果如下:
['hello', 'python', 'world']
??擴(kuò)展??分割后打印還是原字符串(字符串是不可變類型,分割操作是復(fù)制一份原字符串,更改的是復(fù)制出來的那一份)
txt = "hello?python?world" res = txt.split("?") # 打印分割后的 print(res) # 打印原字符串 print(txt)
['hello', 'python', 'world'] hello?python?world
10.strip()
定義 strip()方法刪除字符串兩端的空格 ??舉個(gè)栗子??刪除hello兩端的空格
txt = " hello " res = txt.strip() print(res)
運(yùn)行結(jié)果如下:
hello
11.replace()
定義 replace()方法以指定內(nèi)容替換掉被指定內(nèi)容(默認(rèn)替換全部,可指定替換次數(shù)) ??舉個(gè)栗子??以java替換python
txt = 'hello python world' res = txt.replace('python','java') print(res)
運(yùn)行結(jié)果如下:
hello java world
??擴(kuò)展??替換后打印還是原字符串(字符串是不可變類型,替換操作是復(fù)制一份原字符串,更改的是復(fù)制出來的那一份)
txt = 'hello python world' res = txt.replace('python','java') # 打印替換后的 print(res) # 打印原字符串 print(txt)
hello java world
hello python world
以上就是字符串常用的方法整理
原文鏈接:https://blog.csdn.net/xqe777/article/details/123222475
相關(guān)推薦
- 2022-12-27 Swift?Error重構(gòu)優(yōu)化詳解_Swift
- 2022-06-16 Python實(shí)現(xiàn)視頻下載與合成的示例代碼_python
- 2022-10-17 C++智能指針模板應(yīng)用詳細(xì)介紹_C 語言
- 2022-06-20 go語言實(shí)現(xiàn)屏幕截圖的示例代碼_Golang
- 2022-10-15 Tomcat生命周期詳解_Tomcat
- 2022-04-05 解決IDEA .properties文件中文亂碼的問題
- 2023-11-22 python文件打開時(shí)的訪問模式
- 2022-07-24 Android?Studio工程導(dǎo)入及坑的解決_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支