python002 字串內建函式

2021-08-01 22:01:51 字數 3269 閱讀 5755

str='python string function'

生成字串變數str='python string function'

字串長度獲取:len(str)

例:print '%s length=%d' % (str,len(str))

字母處理

全部大寫:str.upper()

全部小寫:str.lower()

大小寫互換:str.swapcase()

首字母大寫,其餘小寫:str.capitalize()

首字母大寫:str.title()

print '%s lower=%s' % (str,str.lower())

print '%s upper=%s' % (str,str.upper())

print '%s swapcase=%s' % (str,str.swapcase())

print '%s capitalize=%s' % (str,str.capitalize())

print '%s title=%s' % (str,str.title())

格式化相關

獲取固定長度,右對齊,左邊不夠用空格補齊:str.ljust(width)

獲取固定長度,左對齊,右邊不夠用空格補齊:str.rjust(width)

獲取固定長度,中間對齊,兩邊不夠用空格補齊:str.center(width)

獲取固定長度,右對齊,左邊不足用0補齊: str.zfill(width)

print '%s ljust=%s' % (str,str.ljust(20))

print '%s rjust=%s' % (str,str.rjust(20))

print '%s center=%s' % (str,str.center(20))

print '%s zfill=%s' % (str,str.zfill(20))

字串搜尋相關

搜尋指定字串,沒有返回-1:str.find('t')

指定起始位置搜尋:str.find('t',start)

指定起始及結束位置搜尋:str.find('t',start,end)

從右邊開始查詢:str.rfind('t')

搜尋到多少個指定字串:str.count('t')

上面所有方法都可用index代替,不同的是使用index查詢不到會拋異常,而find返回-1

print '%s find nono=%d' % (str,str.find('nono'))

print '%s find t=%d' % (str,str.find('t'))

print '%s find t from %d=%d' % (str,1,str.find('t',1))

print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))

#print '%s index nono ' % (str,str.index('nono',1,2))

print '%s rfind t=%d' % (str,str.rfind('t'))

print '%s count t=%d' % (str,str.count('t'))

字串替換相關

替換old為new:str.replace('old','new')

替換指定次數的old為new:str.replace('old','new',maxreplacetimes)

print '%s replace t to *=%s' % (str,str.replace('t', '*'))

print '%s replace t to *=%s' % (str,str.replace('t', '*',1))

字串去空格及去指定字元

去兩邊空格:str.strip()

去左空格:str.lstrip()

去右空格:str.rstrip()

去兩邊字串:str.strip('d'),相應的也有lstrip,rstrip

str=' python string function '

print '%s strip=%s' % (str,str.strip())

str='python string function'

print '%s strip=%s' % (str,str.strip('d'))

按指定字元分割字串為陣列:str.split(' ')

預設按空格分隔

str='a b c de'

print '%s strip=%s' % (str,str.split())

str='a-b-c-de'

print '%s strip=%s' % (str,str.split('-'))

字串判斷相關

是否以start開頭:str.startswith('start')

是否以end結尾:str.endswith('end')

是否全為字母或數字:str.isalnum()

是否全字母:str.isalpha()

是否全數字:str.isdigit()

是否全小寫:str.islower()

是否全大寫:str.isupper()

str='python string function'

print '%s startwith t=%s' % (str,str.startswith('t'))

print '%s endwith d=%s' % (str,str.endswith('d'))

print '%s isalnum=%s' % (str,str.isalnum())

str='pythonstringfunction'

print '%s isalnum=%s' % (str,str.isalnum())

print '%s isalpha=%s' % (str,str.isalpha())

print '%s isupper=%s' % (str,str.isupper())

print '%s islower=%s' % (str,str.islower())

print '%s isdigit=%s' % (str,str.isdigit())

str='3423'

print '%s isdigit=%s' % (str,str.isdigit())

002字串包含

2014.6.17 題目描述 給定兩個分別由字母組成的字串a和字串b,字串b的長度比字串a短。請問,如何最快地判斷字串b中所有字母是否都在字串a裡?為了簡單起見,我們規定輸入的字串只包含大寫英文本母,請實現函式bool stringcontains string a,string b 比如,如果是下...

sum 轉字串 Python字串與內建函式

字串 建立變數來儲存字串 字串可以通過單 雙 三引號建立字串 message hello,world 變數mseeage,值為 hello,world print message 輸出結果 hello,world python3,有3種數值型別分別為 int 整形 建立變數為a,值為496 a 49...

PYTHON的內建物件 (二)字串物件

最重要的一點 python中的字串為 乙個有序的字元集合 不可變序列 1 python中沒有字元,只有單字元字串。2 字串並不能直接修改某一位置的字元。3 一些重要操作 s1 s2 合併 s1 3 重複 s1 i 索引 s1 i j 分片 len s1 長度 for x in s1 迭代 m in ...