標籤:data- rod enter encode join -- 尋找 情況 phoenix
關於 Python 的字串處理相關的方法還是許多的。因為我正在學習 Python,於是就把 Python 中這些混雜的用於 string 的函數總結出來,在自己忘記的時候便於尋找,希望對於有相似需求的人有所協助。
captalize() 函數功能
將一個字串的第一個字母大寫
使用方法
str.captalize()
參數
無
傳回值
string
示範範例代碼
str = "hello world!"print "str.capitalize(): ", str.capitalize()
執行結果
tr.capitalize(): Hello world!
center(width, fillchar) 函數
將字串置中,置中後的長度為 width
功能
將字串置中。置中後的長度為 width
使用方法
str.center(width[, fillchar])
參數
- width: 表示字串總長度
- fillchar: 使字串置中所填充的字元。默覺得空格
傳回值
返回填充字元後的字串
示範範例代碼
str = "hello world!"print "str.center(20): ", str.center(20)print "str.center(20,‘-‘): ", str.center(20,‘-‘)
執行結果
tr.center(20): hello world!str.center(20,‘-‘): ----hello world!----
count(str, start=0, end=len(string)) 函數功能
返回該字串中出現某字串序列(或字元)的次數
使用方法
str.count(sub, start=0, end=len(string))
參數
- sub: 被尋找的字串序列
- start: 開始尋找的索引位置,默覺得字串開始
- end: 結束尋找的索引位置,默覺得字串結束
傳回值
被尋找的序列在字串的尋找位置中出現的次數
示範範例代碼
str = "hello world! hello world!"sub = "o"print "str.count(sub): ", str.count(sub)sub = "hello"print "str.count(sub, 5) ", str.count(sub, 5)
執行結果
str.count(sub): 4str.count(sub, 5) 1
decode(encoding=’UTF-8’,errors=’strict’) 函數 & encode(encoding=’UTF-8’,errors=’strict’)功能
使用特定編碼將字串解碼(decode)/編碼(encode)
使用方法
str.decode(encoding=‘UTF-8‘,errors=‘strict‘)
str.encode(encoding=‘UTF-8‘,errors=‘strict‘)
參數
- encoding: 使用的編碼格式
- errors: 設定不同的錯誤處理方法,其它選項有
ignore
, replace
, xmlcharrefreplace
, backslashreplace
傳回值
編碼/解碼後的字串
示範範例代碼
str = "hello world!"str = str.encode(‘base64‘, ‘strict‘)print "Encoded str: ", strprint "Decoded str: ", str.decode(‘base64‘)
執行結果
Encoded str: aGVsbG8gd29ybGQhDecoded str: hello world!
endswith(suffix, start=0, end=len(string)) 函數功能
推斷字串是否是以某字串結尾的
使用方法
str.endswith(suffix, start=0, end=len(string))
參數
- suffix: 被尋找的字串
- start: 字串尋找的起始位置,默覺得字串起始位置
- end: 字串尋找的結束位置。默覺得字串結束位置
傳回值
假設字串是以 suffix
結尾的返回 True
, 否則返回 False
示範範例代碼
str = "hello world!"suffix = "world!"print str.endswith(suffix)suffix = "llo"print str.endswith(suffix,0,4)print str.endswith(suffix,0,5)
執行結果
TrueFalseTrue
expandstabs(tabsize=8) 函數功能
提供自己定義 tab(/t) 長度的方法。默覺得8
使用方法
str.expandtabs(tabsize=8)
參數
傳回值
string
示範範例代碼
str = "hello\tworld!"print "Original str: " + strprint "Defalut expanded tab: " + str.expandtabs();print "Double expanded tab: " + str.expandtabs(16)
執行結果
Original str: hello world!Defalut expanded tab: hello world!Double expanded tab: hello world!
find(str, start=0, end=len(string)) 函數功能
在字串的某指定位置尋找某字串
使用方法
str.find(str, start=0, end=len(string))
參數
- str: 被尋找的子字串
- start: 尋找的起始位置,默覺得字串起始位置
- end: 尋找的結束位置。默覺得字串結束位置
傳回值
假設尋找到。返回該子字串的索引。未尋找到。返回-1
示範範例代碼
str = "hello world!"str1 = "wo"print str.find(str1)print str.find(str1, 8)
執行結果
6-1
index(str, start=0, end=len(string)) 函數功能
功能上與 find() 同樣,僅僅是在未找到子字串是拋出異常
使用方法
str.index(str, start=0, end=len(string))
參數
同 find()
傳回值
假設尋找到,返回該子字串的索引;未尋找到,拋出異常
示範範例代碼
str = "hello world!"str1 = "wo"print str.index(str1)print str.index(str1, 8)
執行結果
6Traceback (most recent call last): File "teststrmethods.py", line 6, in <module> print str.index(str1, 8)ValueError: substring not found
isalnum() 函數功能
推斷該字串是否僅僅是字母數字組合
使用方法
str.isalnum()
參數
無
傳回值
假設該字串是字母數字組合,返回 True
,否則返回 False
示範範例代碼
str = "helloworld"print str.isalnum()str = "hello world"print str.isalnum()str = "hello123"print str.isalnum()str = "hello123!"print str.isalnum()
執行結果
TrueFalseTrueFalse
isalpha() 函數功能
推斷該字串是否是字母組合
使用方法
str.isalpha()
參數
無
傳回值
假設該字串是字母組合,返回 True
,否則返回 False
示範範例代碼
str = "helloworld"print str.isalpha()str = "hello world"print str.isalpha()str = "hello123"print str.isalpha()str = "hello123!"print str.isalpha()
執行結果
TrueFalseFalseFalse
isdigit() 函數功能
推斷該字串是否僅僅包括數字
使用方法
str.isdigit()
參數
無
傳回值
假設該字串僅僅包括數字,則返回 True
,否則返回 False
示範範例代碼
str = "hello123"print str.isdigit()str = "123456"print str.isdigit()
執行結果
FalseTrue
islower() 函數功能
推斷該字串中是否僅僅是小寫字母
使用方法
str.islower()
參數
無
傳回值
假設該字串中僅僅是小寫字母,返回True
,否則返回False
示範範例代碼
str = "hello wolrd!"print str.islower()str = "Hello Wolrd!"print str.islower()
執行結果
TrueFalse
isspace() 函數功能
推斷該字串是否僅僅包括空格
使用方法
str.isspace()
參數
無
傳回值
假設該字串僅僅包括空格。返回True
,否則返回False
示範範例代碼
str = " "print str.isspace()str = "Hello Wolrd!"print str.isspace()
執行結果
TrueFalse
istitle() 函數功能
檢查該字串中的單詞是否首字母都大寫
使用方法
str.istitle()
參數
無
傳回值
假設該字串中的單詞首字母都大寫了,返回True
,否則返回False
示範範例代碼
str = "Hello world!"print str.istitle()str = "Hello Wolrd!"print str.istitle()
執行結果
FalseTrue
isupper() 函數功能
推斷該字串中的字母是否都是大寫
使用方法
str.isupper()
參數
無
傳回值
假設該字串中的字母都是大寫,返回True
,否則返回False
示範範例代碼
str = "Hello world!"print str.isupper()str = "HELLO WORLD!"print str.isupper()
執行結果
FalseTrue
join(seq) 函數功能
用該字串串連某字元序列(seq)
使用方法
str.join(sequence)
參數
傳回值
返回串連之後的字串
示範範例代碼
str = "-"sequence = ("hello", "world", "everyone", "!")print str.join(sequence)
執行結果
hello-world-everyone-!
len(string) 函數功能
得到該字串的長度
使用方法
len(str)
參數
無
傳回值
返回該字串長度
示範範例代碼
str = "Hello world!"print "the length of str: ", len(str)
執行結果
the length of str: 12
ljust(width, fillchar=’ ‘)函數功能
在字串的右邊填充字元使得字串達到指定長度
使用方法
str.ljust(width, fillchar=‘ ‘)
參數
- width: 填充後的目標長度
- fillchar: 用於填充的字元。默覺得空格
傳回值
返回填充後的字串
示範範例代碼
str = "Hello world"print str.ljust(15)print str.ljust(15,‘!‘)
執行結果
Hello worldHello world!!!!
本文的著作權歸作者 羅遠航 全部。採用 Attribution-NonCommercial 3.0 License。不論什麼人能夠進行轉載、分享,但不可在未經同意的情況下用於商業用途;轉載請註明出處。感謝配合。
Python 字串 String 內建函數大全(1)