Python學習筆記-字串及操作
![2946773-b14be19c2cad3bbb.png](https://upload-images.jianshu.io/upload_images/2946773-b14be19c2cad3bbb.png)
每一門語言都離不開字串以及字串操作,Python也為字串定義了很多函式。
1、字串切片
sub = string[起始位置:結束為止:步長]
-
獲得子字串
string = "http://www.jianshu.com/p/2cb6159d3f13" print("%s 子字串:%s"%(string, string[7:15]))
-
獲得所有字串
string = "http://www.jianshu.com/p/2cb6159d3f13" print("%s 列印全部是:%s"%(string, string[0:len(string)])) print("%s 列印全部是:%s"%(string, string[0:])) print("%s 列印全部是:%s"%(string, string[:])) print("%s 列印全部是:%s"%(string, string[::]))
-
反向列印字串
string = "http://www.jianshu.com/p/2cb6159d3f13" print("%s 反向列印字串是:%s"%(string, string[::-1]))
2、find查詢字首,rfind查詢字尾
string = "http://www.jianshu.com/p/2cb6159d3f13"
findIndex = string.find(":")
if findIndex == -1:
print("%s 沒有找到的協議"%string)
else:
print("%s 找到的協議是:%s"%(string,string[0:findIndex]))
fileName = "/test/A/a.text"
findIndex = fileName.rfind(".")
if findIndex == -1:
print("檔案%s 沒有找到副檔名"%fileName)
else:
print("檔案%s 找到的副檔名是:%s"%(fileName, fileName[findIndex+1:]))
3、如果使用index和rindex,需要注意,如果沒有子字串,則會出現異常
string = "http://www.jianshu.com/p/2cb6159d3f13"
print("%s 使用的協議是:%s"%(string, string[0:string.index(":")]))
print("%s 使用的協議是:%s"%(string, string[0:string.rindex(":")]))
4、count測試字串重複出現的次數
-
一般用法:
string = "http://www.jianshu.com/p/2cb6159d3f13" subString = "jianshu" print("%s 在‘%s’重複出現了%d次"%(subString, string, string.count(subString)))
-
特殊用法:count測試的時候可以指定起始位置和結束位置
string = "http://www.jianshu.com/p/2cb6159d3f13" subString = "jianshu" print("%s在‘%s’的[%d,%d]區間重複出現了%d次"%(subString, string, 20, 30, string.count(subString, 20, 30)))
5、replace替換字串中的子字串
-
用replace替換字串時,需要注意原字串不改變,替換後的字串以返回形式返回
string = "http://www.jianshu.com/p/2cb6159d3f13" newString = string.replace("http", "https") print("%s 替換協議後是:%s"%(string,newString))
-
用replace預設替換所有匹配的,也可以指定替換幾個
message = "hello world! Ha Ha ha" newMessage1 = message.replace("Ha", "ha") print(newMessage1) newMessage2 = message.replace("Ha", "ha", 1) print(newMessage2)
6、split分隔字串
string = "http://www.jianshu.com/p/2cb6159d3f13"
print(string.split("://"))
7、title:字串每個單詞首字母大寫和capitalize:字串首字母大寫
message = "hello world! Ha Ha ha"
temp1 = message.title()
temp2 = message.capitalize()
print((message, temp1, temp2))
8、startswith和endswith
判斷是否以某個字串開頭或結尾。
string = "http://www.jianshu.com/p/2cb6159d3f13"
if string.startswith("http"):
print("字串是以:http開頭的");
fileName = "/test/A/a.text"
if fileName.endswith("text"):
print("字串是以:text結尾的");
9、lower和upper
將字串以小寫或大寫形式輸出。
message = "hello world! Ha Ha ha"
newMessage1 = message.lower()
newMessage2 = message.upper()
print("%s轉換後為:%s====%s"%(message, newMessage1, newMessage2))
10、ljust、rjust和center
給定字串寬度,輸出按照左對齊,右對齊和中間對齊符串。
message = "hello world! Ha Ha ha"
print(message.ljust(50))
print(message.rjust(50))
print(message.center(50))
11、lstrip、rstrip和strip
輸出去掉字串前、後或者前後空格的字串。
message = " hello world! Ha Ha ha "
print(message.lstrip(" "))
print(message.rstrip(" "))
print(message.strip(" "))
12、partition和rpartition
將字串分成三部分,區別與split。
string = "http://www.jianshu.com/p/2cb6159d3f13"
print(string.partition("://"))
print(string.rpartition("://"))
13、join連結字串
strings = ["hello", "world", "ha"]
strTemp = "*"
strTemp = strTemp.join(strings)
print(strTemp)
以上程式碼可以在GitHub找到。
相關文章
- Python學習筆記:第3天 字串的操作Python筆記字串
- Python學習筆記 - 字串,數字Python筆記字串
- 【Python學習】學習筆記 14-15 字串Python筆記字串
- 字串學習筆記字串筆記
- Python學習筆記|Python之檔案操作Python筆記
- Python 學習筆記(6)— 字串格式化Python筆記字串格式化
- 字串家族 學習筆記字串筆記
- 【學習筆記】字串匹配筆記字串匹配
- Python3學習筆記-字串和編碼Python筆記字串
- Python學習-字串函式操作1Python字串函式
- Python學習-字串函式操作3Python字串函式
- KMP字串匹配學習筆記KMP字串匹配筆記
- python學習筆記:第8天 檔案操作Python筆記
- Python學習筆記-yield用法及優點Python筆記
- Python學習筆記Python筆記
- 【學習筆記】python筆記Python
- C/C++學習筆記:字串C++筆記字串
- Qt學習筆記(五)QString 字串QT筆記字串
- python——Matplotlib學習筆記Python筆記
- python學習筆記4Python筆記
- Python學習筆記(三)Python筆記
- python——numpy學習筆記Python筆記
- python學習筆記(1Python筆記
- Python學習筆記 - queuePython筆記
- Python學習筆記 - asyncioPython筆記
- Python 學習筆記(一)Python筆記
- Python學習筆記 - aiohttpPython筆記AIHTTP
- python學習筆記(二)Python筆記
- Python學習筆記(2)Python筆記
- Effective Python學習筆記Python筆記
- vue常用操作及學習筆記(路由跳轉及路由傳參篇)Vue筆記路由
- 【MongoDB學習筆記】手把手教你配置Python操作MongoDBMongoDB筆記Python
- SQL學習筆記—非select操作SQL筆記
- 【numpy學習筆記】矩陣操作筆記矩陣
- Linux 學習筆記--目錄結構及檔案基本操作Linux筆記
- Python學習筆記|Python之程式Python筆記
- Python學習筆記——turtle庫Python筆記
- python-pygame學習筆記PythonGAM筆記
- 字典--Python學習筆記(五)Python筆記