1. 程式人生 > >python字串使用總結

python字串使用總結

來源: 作者: 靈劍 1.python 字串通常有單引號('...')、雙引號(...)、三引號(...)或('''...''')包圍,三引號包含的字串可由多行組成,一般可表示大段的敘述性字串。在使用時基本沒有差別, 1.python 字串通常有單引號('...')、雙引號("...")、三引號("""...""")或('''...''')包圍,三引號包含的字串可由多行組 成,一般可表示大段的敘述性字串。在使用時基本沒有差別,但雙引號和三引號("""...""")中可以包含單引號,三引號 ('''...''')可以包含雙引號,而不需要轉義。 2.用(/)對特殊字元轉義,如(/)、(')、(")。 3.常用字串函式 1)str.count() //返回該字串中某個子串出現的次數 2)str.find()   //返回某個子串出現在該字串的起始位置 3)str.lower() //將該字串全部轉化為小寫 4)str.upper() //轉為大寫 5)str.split() //分割字串,返回字串串列表,預設以空格分割 6)len(str)     //返回字串長度 例如: >>> str = 'Hello, world' >>> str.count('o') >>> 2 >>> str.find('lo') >>> 3 >>> str.lower() >>> 'hello, world' >>> str.upper() >>> 'HELLO, WORLD' >>> str.split() >>> ['Hello,', 'world'] >>> str.split(',') >>> ['Hello', ' world'] >>> len(str) >>> 13 >>> str >>> 'Hello, world' 以上所有操作都不會改變字串本身! 4.字串與數字相互轉換 import string string.atoi(str[,base]) //base為可選引數,表示將字元轉換成的進位制型別 數字轉換成字串可簡單了,直接用str() 5.字元與ASCII轉換 char->ascii
 ord()
   ascii ->char chr()

======================================================================
#Python字串操作
'' '1.複製字串' ''
#strcpy( sStr1, sStr2)
sStr1 =  'strcpy'
sStr2 =  sStr1
sStr1 =  'strcpy2'
print  sStr2
'' '2.連線字串' ''
#strcat( sStr1, sStr2)
sStr1 =  'strcat'
sStr2 =  'append'

sStr1 + =  sStr2
print  sStr1
'' '3.查詢字元' ''
#strchr( sStr1, sStr2)
sStr1 =  'strchr'
sStr2 =  'r'
nPos =  sStr1. index( sStr2)
print  nPos
'' '4.比較字串' ''
#strcmp( sStr1, sStr2)
sStr1 =  'strchr'
sStr2 =  'strch'
print  cmp( sStr1, sStr2)
'' '5.掃描字串是否包含指定的字元' ''
#strspn( sStr1, sStr2)

sStr1 =  '12345678'
sStr2 =  '456'
#sStr1 and  chars both in  sStr1 and  sStr2
print  len( sStr1 and  sStr2)
'' '6.字串長度' ''
#strlen( sStr1)
sStr1 =  'strlen'
print  len( sStr1)
'' '7.將字串中的小寫字元轉換為大寫字元' ''
#strlwr( sStr1)
sStr1 =  'JCstrlwr'
sStr1 =  sStr1. upper( )
print  sStr1
'' '8.追加指定長度的字串' ''
#strncat( sStr1, sStr2, n)
sStr1 =  '12345'
sStr2 =  'abcdef'
=  3
sStr1 + =  sStr2[ 0: n]
print  sStr1
'' '9.字串指定長度比較' ''
#strncmp( sStr1, sStr2, n)
sStr1 =  '12345'
sStr2 =  '123bc'
=  3
print  cmp( sStr1[ 0: n] , sStr2[ 0: n] )
'' '10.複製指定長度的字元' ''
#strncpy( sStr1, sStr2, n)
sStr1 =  ''
sStr2 =  '12345'
=  3
sStr1 =  sStr2[ 0: n]
print  sStr1
'' '11.字串比較,不區分大小寫' ''
#stricmp( sStr1, sStr2)
sStr1 =  'abcefg'
sStr2 =  'ABCEFG'
print  cmp( sStr1. upper( ) , sStr2. upper( ) )
'' '12.將字串前n個字元替換為指定的字元' ''
#strnset( sStr1, ch, n)
sStr1 =  '12345'
ch =  'r'
=  3
sStr1 =  n *  ch +  sStr1[ 3: ]
print  sStr1
'' '13.掃描字串' ''
#strpbrk( sStr1, sStr2)
sStr1 =  'cekjgdklab'
sStr2 =  'gka'
nPos =  - 1
for  c in  sStr1:
    if  c in  sStr2:
         nPos =  sStr1. index( c)
        break
print  nPos

'' '14.翻轉字串' ''
#strrev( sStr1)
sStr1 =  'abcdefg'
sStr1 =  sStr1[ : : - 1]
print  sStr1
'' '15.查詢字串' ''
#strstr( sStr1, sStr2)
sStr1 =  'abcdefg'
sStr2 =  'cde'
print  sStr1. find( sStr2)
'' '16.分割字串' ''
#strtok( sStr1, sStr2)
sStr1 =  'ab,cde,fgh,ijk'
sStr2 =  ','
sStr1 =  sStr1[ sStr1. find( sStr2)  +  1: ]
print  sStr1

首先要搞清楚,字串在Python內部的表示是unicode編碼.

因此,在做編碼轉換時,通常需要以unicode作為中間編碼,即先將其他編碼的字串解碼(decode)成unicode,再從unicode編碼(encode)成另一種編碼。

decode的作用是將其他編碼的字串轉換成unicode編碼,

如str1.decode('gb2312'),表示將gb2312編碼的字串轉換成unicode編碼。

encode的作用是將unicode編碼轉換成其他編碼的字串,

如str2.encode('gb2312'),表示將unicode編碼的字串轉換成gb2312編碼。

在某些IDE中,字串的輸出總是出現亂碼,甚至錯誤,其實是由於IDE的結果輸出控制檯自身不能顯示字串的編碼,而不是程式本身的問題。

如在UliPad中執行如下程式碼:

s=u"中文"

print s

會提示:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)。

這是因為UliPad在英文WindowsXP上的控制檯資訊輸出視窗是按照ascii編碼輸出的(英文系統的預設編碼是ascii),而上面程式碼中的字串是Unicode編碼的,所以輸出時產生了錯誤。

將最後一句改為:print s.encode('gb2312') 則能正確輸出“中文”兩個字。

若最後一句改為:print s.encode('utf8') 則輸出:/xe4/xb8/xad/xe6/x96/x87,

這是控制檯資訊輸出視窗按照ascii編碼輸出utf8編碼的字串的結果。

另外,程式碼中字串的預設編碼與程式碼檔案本身的編碼一致,

如: s='中文' 如果是在utf8的檔案中,該字串就是utf8編碼,如果是在gb2312的檔案中,則其編碼為gb2312。這種情況下,要進行編碼轉換,都需要先用decode方法將其轉換成unicode編碼,再使用encode方法將其轉換成其他編碼。

通常,在沒有指定特定的編碼方式時,都是使用的系統預設編碼建立的程式碼檔案,在這篇文章中可以看到如何獲得系統的預設編碼。

如果字串是這樣定義: s=u'中文' 則該字串的編碼就被指定為unicode了,即python的內部編碼,而與程式碼檔案本身的編碼無關。

因此,對於這種情況做編碼轉換,只需要直接使用encode方法將其轉換成指定編碼即可。

如果一個字串已經是unicode了,再進行解碼則將出錯,

因此通常要對其編碼方式是否為unicode進行判斷:

isinstance(s, unicode) #用來判斷是否為unicode