Python操作字串(2)

來源:互聯網
上載者:User

標籤:可見   api   exp   number   example   ssl   contains   eal   方法   

字串的常用操作包括但不限於以下操作:

字串的替換、刪除、截取、複製、串連、比較、尋找、分割等

這裡將對字串的內建操作方法進行總結歸納,重點是以樣本的方式進行展示。

使用type擷取建立對象的類 type(name)使用dir擷取類的成員dir(name)使用vars擷取類的成員和各個成員的值

capitalize

    功能:字串首字母大寫    name = ‘swhthaitun‘    name.capitalize()    返回結果:‘Swhthaitun‘

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

Python操作字串(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.