字符串的常用操作包括但不限於以下操作:
字符串的替換、刪除、截取、復制、連接、比較、查找、分割等
這里將對字符串的內置操作方法進行總結歸納,重點是以示例的方式進行展示。
使用type獲取創建對象的類 type(name)
使用dir獲取類的成員dir(name)
使用vars獲取類的成員和各個成員的值
capitalize
功能:字符串首字母大寫
name = 'swhthaitun'
name.capitalize()
返回結果:'Swht'
casefold()首字母小寫
name = 'HelloWord'
reault = name.casefold()
print(reault)
返回結果:helloword
casefold
功能:將字符串中所有的大寫字母轉換成小寫字母
s1 = "['bsondump', 'mongo', 'mongod', 'mongodump', 'mongoexport', 'mongofiles', 'mongoimport', 'mongooplog', 'mongoperf', 'mongoLLKJKKore', 'mongos', 'UUUngostat', 'monGGtop']"
s1.casefold()
返回結果:"['bsondump', 'mongo', 'mongod', 'mongodump', 'mongoexport', 'mongofiles', 'mongoimport', 'mongooplog', 'mongoperf', 'mongollkjkkore', 'mongos', 'uuungostat', 'monggtop']"
center
功能:字符串寬度填充,使用原有字符串+填充字符構成指定長度的新的字符串
name = 'swhthaitun'
name.center(15)
返回結果:' swhthaitun ' #默認以空格進行填充
name.center(16,'*')
返回結果:'***swhthaitun***'
功能:字符串居中,以‘*’分割(20為新產生字符串的總的寬度)
name = 'HelloWord'
reault = name.center(20,'*')
print(reault)
返回結果:*****HelloWord******
count
功能:統計某個字符在字符串中出現的次數,或在字符串指定區間內完成上述操作
name = 'swhthaitun'
name.count('h')
返回結果:2
name.count('h',0,3) #從索引值0-3范圍的字符中統計'h'出現的次數
返回結果:1
功能:統計子序列出現的次數
name = 'HelloWord'
reault = name.count('W') #如果換成'w',返回結果為0,python對大小寫敏感
print(reault)
返回結果:1
name = 'HelloWord'
reault = name.count('l',0,3) #統計單個字符出現的次數,可以指定起始范圍,另外在python中起始范圍講究顧頭不顧尾的原則,即[0,3)
print(reault)
encode
功能:對字符串進行編碼操作
name = 'swhthaitun'
name.encode()
返回結果:b'swhthaitun'
功能:轉變字符串的編碼
name = '南非波波'
reault = name.encode('gbk')
print(reault)
返回結果:b'\xc4\xcf\xb7\xc7\xb2\xa8\xb2\xa8'
endswith
功能:判斷字符串是否以某個字符串結尾的,返回值為bool型
name = 'swhthaitun'
name.endswith('s')
返回結果:False
name.endswith('n')
返回結果:True
name.endswith('tun')
返回結果:True
name = 'Iamalatterboy'
reault = name.endswith('y')
print(reault)
返回結果:True
expandtabs
功能:將制表符'\t'轉換成指定寬度的tab鍵分割,默認tabsize=8
li = 'sw\tht'
li.expandtabs(4)
返回結果:'sw ht'
li.expandtabs()
返回結果:'sw ht'
find
功能:在字符串中查找指定字符串,找不到時返回-1
name = 'swht'
name.find('s')
返回結果:0
name.find('h')
返回結果:2
format
功能:格式化輸出字符串
li = 'I\'m {},{}' #兩個'{}'是占位符
li.format('swht','歡迎來中國')
返回結果:"I'm swht,歡迎來中國"
參考:http://blog.chinaunix.net/uid-23802873-id-4477364.html
_contains_
功能:包含 -->'eal' in name
name = 'swhtkkskjj'
reault = name.__contains__('swht')
print(reault)
返回結果:True
index
功能:在字符串中查找指定的字符串,找不到時直接報錯
name = 'swhthaitun'
name.index('w')
返回結果:1
join()
功能:字符串連接
name = 'swhthaitun'
'*'.join(name)
返回結果:'s*w*h*t*h*a*i*t*u*n'
isalnum
功能:檢查判斷字符串是否包含字母數字字符(http://www.yiibai.com/python/string_isalnum.html)
name = 'swhthaitun'
name.isalnum()
返回結果:True
isalpha
功能:檢測字符串是否只由字母組成(http://www.runoob.com/python/att-string-isalpha.html)
name = 'swhthaitun'
name.isalpha()
返回結果:True
isdecimal
功能:檢查字符串是否只包含十進制字符。這種方法只存在於unicode對象。(參考:http://www.runoob.com/python/att-string-isdecimal.html)
name = 'swhthaitun'
name.isdecimal()
返回結果:False
isdigit
功能:檢測字符串是否只由數字組成。(參考:http://www.runoob.com/python/att-string-isdigit.html)
name = 'swhthaitun'
name.isdigit()
返回結果:False
isidentifier
功能:檢測字符串是否是字母開頭
name = 'swhthaitun'
name.isidentifier()
返回結果:True
name = '1swhthaitun'
name.isidentifier()
返回結果:False
isnumeric
功能:檢測字符串是否只由數字組成。這種方法是只針對unicode對象。
name = 'swhthaitun'
name.isnumeric()
返回結果:False
Li = '5523'
Li.isnumeric()
返回結果:True
isprintable
功能:判斷字符串中所有字符是否都屬於可見字符
a = "\tPuppy"
a.isprintable()
返回結果:False
name = 'swhthaitun'
name.isprintable()
返回結果:True
isspace
功能:檢測字符串是否為空格
name = 'swhthaitun'
name.isspace()
返回結果:False
Li = ' '
Li.isspace()
返回結果:True
istitle
功能:判斷字符串是否適合當作標題(其實就是每個單詞首字母大寫)
a = "a puppy"
b = "Puppy"
a.istitle()
返回結果:False
b.istitle()
返回結果:True
isupper
功能:判斷字符串中所有字母字符是否都是大寫字母
a = "puppy"
b = "PUPPY"
a.isupper()
返回結果:False
b.isupper()
返回結果:True
ljust
功能:返回一個原字符串左對齊,並使用空格填充至指定長度的新字符串。如果指定的長度小於原字符串的長度則返回原字符串。(參考:http://www.runoob.com/python/att-string-ljust.html)
語法:str.ljust(width[, fillchar])
width -- 指定字符串長度。
fillchar -- 填充字符,默認為空格。
name = 'swhthaitun'
name.ljust(50,'*')
返回結果:'swhthaitun****************************************'
lower
功能:將所有的字母轉換成小寫字母
name = 'SWHT'
name.lower()
返回結果:'swht'
lstrip
功能:去除字符串左邊開頭的空格
name = ' swht '
name.lstrip()
返回結果:'swht '
rstrip
功能:去除字符串右邊結尾的空格
name = ' swht '
name.rstrip()
返回結果:' swht'
strip
功能:去除字符串兩邊的空格
name = ' swht '
name.rstrip()
返回結果:'swht'
maketrans
功能:用於創建字符映射的轉換表,對於接受兩個參數的最簡單的調用方式,第一個參數是字符串,表示需要轉換的字符,第二個參數也是字符串表示轉換的目標。
注:兩個字符串的長度必須相同,為一一對應的關系。
語法:str.maketrans(intab, outtab)
參數:intab -- 字符串中要替代的字符組成的字符串。
outtab -- 相應的映射字符的字符串。
intab = "swhtr"
outtab = "12345"
name = "hjjksknsnjmk"
name.maketrans(intab, outtab)
返回結果:{104: 51, 114: 53, 115: 49, 116: 52, 119: 50}
partition
功能:根據指定的分隔符將字符串進行分割。
如果字符串包含指定的分隔符,則返回一個3元的元組,第一個為分隔符左邊的子串,第二個為分隔符本身,第三個為分隔符右邊的子串。
name = 'swht'
li = 'hhsslswhtolljm'
li.partition(name)
返回結果:('hhssl', 'swht', 'olljm')
replace
功能:把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數max,則替換不超過 max 次。
語法:str.replace(old, new[, max])
參數:old -- 將被替換的子字符串。
new -- 新字符串,用於替換old子字符串。
max -- 可選字符串, 替換不超過 max 次
str = "this is string example....wow!!! this is really string"
str.replace("is", "was")
返回結果:'thwas was string example....wow!!! thwas was really string'
str.replace("is", "was", 3)
返回結果:'thwas was string example....wow!!! thwas is really string'
split
功能:字符串分割,默認是空格
name.split()
返回結果:['swht']
name.split('s') #以's'字符進行分割
返回結果:['', 'wht']
_add_
功能:在字符串后面增加指定的字符或字符串
name = 'swht'
name.__add__('e')
返回結果:'swhte'
li = 'hjh'
name.__add__(li)
返回結果:'swhthjh'
_contains_
功能:判斷指定字符串是否包含在字符串中,返回值為True和False
name = 'swht'
name.__contains__('s')
返回結果:True
_eq_
功能:判斷字符串是否相等,返回值為True和False
name = 'swht'
li = 'test'
name.__eq__(li)
返回結果:False