python中字串 str 的常用處理方法

2021-07-10 17:40:35 字數 3300 閱讀 4436

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.ljust(width)

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

獲取固定長度,右對齊,左邊不足用0補齊

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())

還有其他常見的python字串處理 函式的話不定期更新。

python中字串(str)的操作

s hello 字串的重疊 s hello 2 字串的拼接 s hello world print s 統計字元個數 print len s 提取單個字元,通過下表提取 從開頭提取,下表從0開始 print s 0 從結尾提取,下表從 1開始 print s 1 切片 s 開始 結束 步進 s ab...

python學習 str字串

s hello world print s s hello world print s s hello world print s 轉義字元案例 想表達let s go 使用轉義字元 s let s go 就想表達乙個單引號,不想組成引號對 print s 表示斜槓 比如表示c user augsn...

python基礎 字串(str

標準序列的常規操作 索引 切片 乘法 成員資格檢查 長度等 適用於字串,但字串是不可變的資料型別,因此元素賦值和切片賦值是非法的。這裡介紹字串兩個方面 字串格式設定 字串方法 使用字串格式設定運算子 並在 右邊指定格式的值。指定要設定其格式的值時,可使用單個值 如字串,數字等 亦可使用元組 設定多個...