python(5)字符串處理 (sub,replace,find,index,upper,strip,split,sub翻頁

一,sub和replace的用法python

re.sub 函數進行以正則表達式爲基礎的替換工做正則表達式

re.sub替換到目標字符串中的a,b或者c,並所有替換api

 另加上sub翻頁操做:數組

re.sub('start=\d+','start=%d'%i,url,re.S)
1 >>> import re
2 >>> re.sub('[abc]','o','Mark')
3 'Mork'
4 >>> re.sub('[abc]','o','caps')
5 'oops'
6 >>

replace 用法介紹:函數

1 >>> a
2 'asds23DFG34'
3 >>> a.replace('s','M')   #所有替換
4 'aMdM23DFG34'
5 >>> b = 'adfafafafa'
6 >>> b.replace('a','M',3) #指定個數的替換
7 'MdfMfMfafa'

二,find和index的用法oop

index,find 返回的都是找到的字符串的下標;find若是找不到返回的值 則是-1,而index直接拋出異常url

a.find('t',start)從起始位置搜索spa

a.find('t',start,end)從指定位置開始搜索3d

a.rfind('t')從右邊位置開始搜索code

a.count('t') 搜索到多少個指定的字符

1 >>> a = '12432423'
2 >>> a.find('1')
3 0
4 >>> a.find(5)
5 Traceback (most recent call last):
6   File "<stdin>", line 1, in <module>
7 TypeError: expected a character buffer object
8 >>> a.find('24')
9 1
 1 >>> a
 2 '12432423'
 3 >>> f = a.find('M')
 4 >>> f
 5 -1
 6 >>> f = a.find('43') #返回的是字符串的第一個位置
 7 >>> f
 8 2
 9 >>> f = a.find('435')
10 >>> f
11 -1
 1 '12432423'
 2 >>> a.index('4')
 3 2
 4 >>> a.index('8')
 5 Traceback (most recent call last):
 6   File "<stdin>", line 1, in <module>
 7 ValueError: substring not found
 8 >>> f  = a.index('8')
 9 Traceback (most recent call last):
10   File "<stdin>", line 1, in <module>
11 ValueError: substring not found

三,大小寫處理,upper,lower,swapcase,capitalize,title

 1 >>> a
 2 '123dfsdfs'
 3 >>> a = 'asds23DFG34'
 4 >>> a.upper()          #所有大寫
 5 'ASDS23DFG34'
 6 >>> a.lower()          #所有小寫
 7 'asds23dfg34'
 8 >>> a.swapcase()       #大小寫交換
 9 'ASDS23dfg34'
10 >>> a.capitalize()     #首字母大寫其他小寫
11 'Asds23dfg34'
12 >>> a.title()           
13 'Asds23Dfg34'          #子串,首字母大寫
14 >>>
15 KeyboardInterrupt

 四,strip的用法

Python中的strip用於去除字符串的首尾字符,同理,lstrip用於去除左邊的字符,rstrip用於去除右邊的字符。

這三個函數均可傳入一個參數,指定要去除的首尾字符。

須要注意的是,傳入的是一個字符數組,編譯器去除兩端全部相應的字符

 1 >>> a
 2 'asds23DFG34'
 3 >>> a.strip('a')
 4 'sds23DFG34'
 5 >>> a.strip('s')
 6 'asds23DFG34'
 7 >>> s = 'saaaay yes no yaaaass'
 8 >>> s.strip('say')        #兩邊都找
 9 ' yes no '                   #兩邊各有一個空格
10 >>> s.lstrip('say')       #只找左邊
11 ' yes no yaaaass'        
12 >>> s.rstrip('say')      #只找右邊
13 'saaaay yes no '

 五,split的用法

Python split()經過指定分隔符對字符串進行切片

語法 str.split('分隔符',num)

  • str -- 分隔符,默認爲空格。
  • num -- 分割次數。
  • 返回分割後的字符串列表
  • 結果一:把\n歸爲了換行符,因此直接去掉了
  • #!/usr/bin/python
    
    str = "Line1-abcdef \nLine2-abc \nLine4-abcd"
    print str.split( )
    print str.split(' ', 1 )
    
    以上實例輸出結果以下:
    ['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
    ['Line1-abcdef', '\nLine2-abc \nLine4-abcd']

     樣例:

>>> str = "asd \dfa \dadf"
>>> str.split()
['asd', '\\dfa', '\\dadf']    #自動增長了換行符
>>> str = "asd \\dfa \\dadf"
>>> str.split()
['asd', '\\dfa', '\\dadf']
>>> aa = str.split()
>>> aa
['asd', '\\dfa', '\\dadf']
>>> aa[0]
'asd'
>>> aa[1]
'\\dfa'
>>> aa[2]
'\\dadf'
>>> bb = r"asdf \bsdf \fsdfe"
>>> bb.split()
['asdf', '\\bsdf', '\\fsdfe']
>>> print aa[0]  #自動去掉轉義
asd
>>> print aa[1]
\dfa
>>> print aa[2]
\dadf
>>>
相關文章
相關標籤/搜索