標籤:拋出異常 idt sub 組成 類型 搜尋 swa 結構 結束
一、過濾字串1、strip()(1)去掉行收尾不可見字元a = ‘ wejifrow ‘print aprint a.strip()結果:wejifrowwejifrow(2)strip([chars])a = ‘**weji**frow**‘print a.strip(‘*‘)結果:weji**frow(3)lstrip([chars]) rstrip([chars])s=‘* dwiefi **‘print s.lstrip(‘*‘)print s.rstrip(‘*‘)結果:dwiefi *** dwiefi str1=r‘\n\t andwf\n\t ‘print str1print str1.strip()print str1.strip(‘\tf\n ‘)結果:\n\t andwf\n\t\n\t andwf\n\t\n\t andwf\n\t 二、大小寫互換1、ASCII值來換 ord chr2、s1.lower() 大寫轉換為小寫s2.upper() 小寫轉換成大寫s3.swapcase() 大小寫互換3、句子的第一個單詞的首字母轉換為大寫 s4.capitalize()句子的所有單詞首字母都需要大寫(模組方法):import stringstring.capwords(s5)或者s5.title() 4、string模組import stringstring.uppercasestring.lowercase 三、字串對齊1、s.ljust(width,[fillchar]) 靠左對齊s.rjust(width) 靠右對齊width定義長度fillchar:使用什麼字元來補齊s=‘123abc‘print s.ljust(10,‘*‘)結果:123abc**** print ‘lilili‘.ljust(15),‘female‘.ljust(10),‘23‘.ljust(5),‘zhouzhou‘.ljust(15)與print ‘%-15s %-10s %-5s %-15s‘%(‘lili‘,‘女‘,‘23‘,‘zhouzhou‘)效果一樣2、中間對齊 s.centerwidth,[fillchar])print s.center(15,‘*‘)結果:*****123abc**** 3、s.zfill(width)s.zfill(10) ==s.rjust(10,‘0‘)結果:0000123abc 四、搜尋1、s.find(substr,[start,[end]])substr在s字元中出現的第一個的下標start end:從s字串的下標為start的位置開始,下標為end-1的位置結束沒有找到 返回 -1 2、s.index(substr,[start,[end]])說明:[start,[end]] 有end的情況,必須有startfind與index方法的區別:find:返回-1index:如果沒有找到,會拋出異常:ValueError: substring not found使用會拋異常的 做異常分支處理 (APP崩潰經常是沒有捕獲這類異常引起的)try:passexcept: 3、s.rfind(subStr,[start[,end]])從字串的右側進行尋找s=‘boy boy‘print s.find(‘boy‘)print s.rfind(‘boy‘)結果:04 4、s.rindex(subStr,[start[,end]])s=‘boy boy‘print s.rindex(‘boy‘)結果:4 五、字串替換1、s.replace(oldStr,newStr,[count])使用newStr替換oldStr。count:替換多少次沒有傳回值,需要s=s.replace() 2、s.expandtabs([tabsize])把定位字元替換成空格tabsize:預設一個tab鍵替換為8個空格, 把一個tab鍵替換成tabsize個空格 六、字串切割1、s.split([sep[,maxsplit]])sep:以s字串中的seq字串來分隔maxsplit:從左往右分隔的次數 2、s.splitlines([keepends])分隔行,以分行符號(\n)keepends=true,保留分行符號。預設值為False 3、s.rsplit([sep[,maxsplit]])從字串右側開始切割 七、列錶轉換為字串1、s.join()由字串組成的列表拼接成字串 2、s.startwith(subStr)字串s是否以subStr子串開頭,傳回值 true or falses.endswith(subStr)字串s是否以subStr子串結尾,傳回值 true or false 3、s1 in s判斷s1子串是否在字串s中 八、判斷字串1、s.isalpha()s字串是否都是有字母組成,並且至少由一個字元組成 2、s.isalnum()s字串是由字母或者數字組成,並且至少由一個字元組成 3、s.isdigit()s字串中的字元都是由數字組成的,並且至少由一個字元組成‘12.23’.isdigit() 結果是FALSE 4、s.isspace()判斷s字串是否由空格組成,並且至少由一個字元組成只要是空白的,就認為是空格 5、s.islower()判斷字串s中的字母(可以包含其他的字元)是否都是小寫,並且至少有一個字母TRUE or Flase‘a1’.islower() 結果為True 6、s.isupper()判斷字串s中的字母(可以包含其他的字元)是否都是大寫,並且至少有一個字母 7、s.istitle()判斷字串s中的所有單詞的首字母是大寫,其他字母為小寫,並且至少有一個字母 8、import stringstring.maketrans(from,to)映射表,做編碼和解碼的時候使用例如:mapTable=string.maketrans(‘123’,‘abc’)s=‘111abc123abc‘print s.translate(mapTable)結果:aaaabcabcabc 九、數字類型的轉換int()float()long()import string1、string.atoi(s,a) 還可以轉換八進位,十六進位a:代表的是進位數s=‘18‘print int(s)print string.atoi(s)結果:1818print string.atoi(‘011‘,8) 9print string.atoi(‘0x11‘,16) 17 2、string.atol(‘11‘) 113、string.atof(‘1.23456‘) 1.23456 十、1、ord() 字母轉換為對應的ASCII值2、chr() 把數字轉換成對應的字元3、s.encode([encoding[,errors]]) 編碼函數 把Unicode編碼為需要的encoding:編碼的類型errors:預設值’strict‘。ignore(忽略)s.decode() 解碼函數 解碼為Unicode格式
Python之資料結構:字串中的方法