python字串使用總結
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'
n =
3
sStr1 +
=
sStr2[
0:
n]
print
sStr1
''
'9.字串指定長度比較'
''
#strncmp(
sStr1,
sStr2,
n)
sStr1 =
'12345'
sStr2 =
'123bc'
n =
3
print
cmp(
sStr1[
0:
n]
,
sStr2[
0:
n]
)
''
'10.複製指定長度的字元'
''
#strncpy(
sStr1,
sStr2,
n)
sStr1 =
''
sStr2 =
'12345'
n =
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'
n =
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