python字串內建函式詳解
概述
字串方法是從python1.6到2.0慢慢加進來的——它們也被加到了Jython中。
這些方法實現了string模組的大部分方法,如下表所示列出了目前字串內建支援的方法,所有的方法都包含了對Unicode的支援,有一些甚至是專門用於Unicode的。
Python capitalize()方法
描述
Python capitalize()將字串的第一個字母變成大寫,其他字母變小寫。對於 8 位位元組編碼需要根據本地環境。
語法
capitalize()方法語法: str.capitalize()
引數
無。
返回值
該方法返回一個首字母大寫的字串。
例項
以下例項展示了capitalize()方法的例項:
#!/usr/bin/python
str = "this is string example....wow!!!";
print "str.capitalize() : ", str.capitalize()
以上例項輸出結果如下:
str.capitalize() : This is string example....wow!!!
Python center()方法
描述
Python center() 返回一個原字串居中,並使用空格填充至長度 width 的新字串。預設填充字元為空格。
語法
center()方法語法: str.center(width[, fillchar])
引數
width – 字串的總寬度。 fillchar – 填充字元。
返回值
該方法返回一個原字串居中,並使用空格填充至長度 width 的新字串。
例項
以下例項展示了center()方法的例項:
#!/usr/bin/python
str = "this is string example....wow!!!";
print "str.center(40, 'a') : ", str.center(40, 'a')
以上例項輸出結果如下:
str .center(40, 'a') : aaaathis is string example....wow!!!aaaa
Python count()方法
描述
Python count() 方法用於統計字串裡某個字元出現的次數。可選引數為在字串搜尋的開始與結束位置。
語法
count()方法語法: str.count(sub, start= 0,end=len(string))
引數
sub – 搜尋的子字串
start – 字串開始搜尋的位置。預設為第一個字元,第一個字元索引值為0。
end –字串中結束搜尋的位置。字元中第一個字元的索引為 0。預設為字串的最後一個位置。
返回值
該方法返回子字串在字串中出現的次數。
例項
以下例項展示了count()方法的例項:
#!/usr/bin/python
str = "this is string example....wow!!!";
sub = "i";
print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)
sub = "wow";
print "str.count(sub) : ", str.count(sub)
以上例項輸出結果如下:
str.count(sub, 4, 40) : 2
str.count(sub, 4, 40) : 1
Python decode()方法
描述
Python decode() 方法以 encoding 指定的編碼格式解碼字串。預設編碼為字串編碼。
語法
decode()方法語法:
str.decode(encoding=’UTF-8’,errors=’strict’)
引數
encoding – 要使用的編碼,如”UTF-8”。
errors – 設定不同錯誤的處理方案。預設為
‘strict’,意為編碼錯誤引起一個UnicodeError。 其他可能得值有 ‘ignore’, ‘replace’,’xmlcharrefreplace’, ‘backslashreplace’ 以及通過 codecs.register_error() 註冊的任何值。
返回值
該方法返回解碼後的字串。
例項
以下例項展示了decode()方法的例項:
#!/usr/bin/python
str = "this is string example....wow!!!";
str = str.encode('base64','strict');
print "Encoded String: " + str;
print "Decoded String: " + str.decode('base64','strict')
以上例項輸出結果如下:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
Python encode()方法
描述
Python encode() 方法以 encoding 指定的編碼格式編碼字串。errors引數可以指定不同的錯誤處理方案。
語法
str.encode(encoding=’UTF-8’,errors=’strict’)
引數
encoding – 要使用的編碼,如”UTF-8”。
errors – 設定不同錯誤的處理方案。預設為 ‘strict’,意為編碼錯誤引起一個UnicodeError。 其他可能得值有’ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ 以及通過 codecs.register_error() 註冊的任何值。
返回值
該方法返回編碼後的字串。
例項
以下例項展示了encode()方法的例項:
#!/usr/bin/python
str = "this is string example....wow!!!";
print "Encoded String: " + str.encode('base64','strict')
以上例項輸出結果如下:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Python endswith()方法
描述
Python endswith()方法用於判斷字串是否以指定字尾結尾,如果以指定字尾結尾返回True,否則返回False。
可選引數”start”與”end”為檢索字串的開始與結束位置。
語法
endswith()方法語法: str.endswith(suffix[, start[, end]])
引數
suffix – 該引數可以是一個字串或者是一個元素。
start – 字串中的開始位置。
end – 字元中結束位置。
返回值
如果字串含有指定的字尾返回True,否則返回False。
例項
以下例項展示了endswith()方法的例項:
#!/usr/bin/python
str = "this is string example....wow!!!";
suffix = "wow!!!";
print str.endswith(suffix);
print str.endswith(suffix,20);
suffix = "is";
print str.endswith(suffix, 2, 4);
print str.endswith(suffix, 2, 6);
以上例項輸出結果如下:
True
True
True
False
Python expandtabs()方法
描述
Python expandtabs() 方法把字串中的 tab 符號(‘\t’)轉為空格,tab 符號(‘\t’)預設的空格數是 8。
語法
expandtabs()方法語法: str.expandtabs(tabsize=8)
引數
tabsize – 指定轉換字串中的 tab 符號(‘\t’)轉為空格的字元數
返回值
該方法返回字串中的 tab 符號(‘\t’)轉為空格後生成的新字串。
例項
以下例項展示了expandtabs()方法的例項:
#!/usr/bin/python
str = "this is\tstring example....wow!!!";
print "Original string: " + str;
print "Defualt exapanded tab: " + str.expandtabs();
print "Double exapanded tab: " + str.expandtabs(16);
以上例項輸出結果如下:
Original string: this is string example....wow!!!
Defualt exapanded tab: this is string example....wow!!!
Double exapanded tab: this is string example....wow!!!
Python find()方法
描述
Python find() 方法檢測字串中是否包含子字串 str ,如果指定 beg(開始) 和 end(結束) 範圍,則檢查是否包含在指定範圍內,如果包含子字串返回開始的索引值,否則返回-1。
語法
find()方法語法: str.find(str, beg=0, end=len(string))
引數
str – 指定檢索的字串
beg – 開始索引,預設為0。
end – 結束索引,預設為字串的長度。
返回值
如果包含子字串返回開始的索引值,否則返回-1。
例項
以下例項展示了find()方法的例項:
#!/usr/bin/python
str1 = "this is string example....wow!!!";
str2 = "exam";
print str1.find(str2);
print str1.find(str2, 10);
print str1.find(str2, 40);
以上例項輸出結果如下:
15
15
-1
Python index()方法
描述
Python index() 方法檢測字串中是否包含子字串 str ,
如果指定 beg(開始) 和 end(結束)範圍,則檢查是否包含在指定範圍內
該方法與 python find()方法一樣,只不過如果str不在 string中會報一個異常。
語法
index()方法語法: str.index(str, beg=0, end=len(string))
引數
str – 指定檢索的字串
beg – 開始索引,預設為0。
end – 結束索引,預設為字串的長度。
返回值
如果包含子字串返回開始的索引值,否則丟擲異常。
例項
以下例項展示了index()方法的例項:
#!/usr/bin/python
str1 = "this is string example....wow!!!";
str2 = "exam";
print str1.index(str2);
print str1.index(str2, 10);
print str1.index(str2, 40);
以上例項輸出結果如下:
15
15
Traceback (most recent call last):
File "test.py", line 8, in
print str1.index(str2, 40);
ValueError: substring not found
shell returned 1
異常處理的部分後續再說~