Python學習筆記-字串及操作

weixin_34208283發表於2017-12-03
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找到。

相關文章