1、去空格及特殊符號
s.strip().lstrip().rstrip(',')
(1)strip():把頭和尾的空格去掉
(2)lstrip():把左邊的空格去掉
(3)rstrip():把右邊的空格去掉
(4)replace('c1','c2'):把字串裡的c1替換成c2。故可以用replace(' ','')來去掉字串裡的所有空格
(5)split():通過指定分隔符對字串進行切片,如果引數num 有指定值,則僅分隔 num 個子字串
2、複製字串
#strcpy(sstr1,sstr2)
sstr1 = 'strcpy'
sstr2 = sstr1
sstr1 = 'strcpy2'
print sstr2
3、連線字串
#strcat(sstr1,sstr2)
sstr1 = 'strcat'
sstr1 += sstr2
print sstr1
4、查詢字元
#strchr(sstr1,sstr2)
# < 0 為未找到
sstr1 = 'strchr'
sstr2 = 's'
npos = sstr1.index(sstr2)
print npos
5、比較字串
#strcmp(sstr1,sstr2)
sstr1 = 'strchr'
sstr2 = 'strch'
print cmp(sstr1,sstr2)
6、掃瞄字串是否包含指定的字元
#strspn(sstr1,sstr2)
sstr1 = '12345678'
sstr2 = '456'
#sstr1 and chars both in sstr1 and sstr2
print len(sstr1 and sstr2)
7、字串長度
#strlen(sstr1)
sstr1 = 'strlen'
print len(sstr1)
8、將字串中的大小寫轉換
s.lower() #小寫
s.upper() #大寫
s.swapcase() #大小寫互換
s.capitalize() #首字母大寫
string.capwords(s) #這是模組中的方法。它把s用split()函式分開,然後用capitalize()把首字母變成大寫,最後用join()合併到一起
#例項:
#strlwr(sstr1)
sstr1 = 'jcstrlwr'
sstr1 = sstr1.upper()
#sstr1 = sstr1.lower()
print sstr1
9、追加指定長度的字串
#strncat(sstr1,sstr2,n)
sstr1 = '12345'
sstr2 = 'abcdef'
n = 3
sstr1 += sstr2[0:n]
print sstr1
10、字串指定長度比較
#strncmp(sstr1,sstr2,n)
sstr1 = '12345'
sstr2 = '123bc'
n = 3
print cmp(sstr1[0:n],sstr2[0:n])
11、複製指定長度的字元
#strncpy(sstr1,sstr2,n)
sstr1 = ''
sstr2 = '12345'
n = 3
sstr1 = sstr2[0:n]
print sstr1
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
#或者s = 'ab,cde,fgh,ijk'
print(s.split(','))
17、連線字串:
delimiter = ','
mylist = ['brazil', 'russia', 'india', 'china']
print delimiter.join(mylist)
18、php 中 addslashes 的實現
def addslashes(s):
d =
return ''.join(d.get(c, c) for c in s)
s = "john 'johny' doe (a.k.a. \"super joe\")\\\0"
print s
print addslashes(s)
19、只顯示字母與數字
def onlycharnum(s,oth=''):
s2 = s.lower();
fomart = 'abcdefghijklmnopqrstuvwxyz0123456789'
for c in s2:
if not c in fomart:
s = s.replace(c,'');
return s;
print(onlystr("a000 aa-b"))
20、擷取字串
str = '0123456789′
print str[0:3] #擷取第一位到第三位的字元
print str[:] #擷取字串的全部字元
print str[6:] #擷取第七個字元到結尾
print str[:-3] #擷取從頭開始到倒數第三個字元之前
print str[2] #擷取第三個字元
print str[-1] #擷取倒數第乙個字元
print str[::-1] #創造乙個與原字串順序相反的字串
print str[-3:-1] #擷取倒數第三位與倒數第一位之前的字元
print str[-3:] #擷取倒數第三位到結尾
print str[:-5:-3] #逆序擷取
Python字串操作
1 複製字串 str2 str1 2 鏈結字串 str abc 3 查詢字串 string.find sub string.index sub string.rfind sub string,rindex sub 4 字串比較 cmp str1,str2 cmp str1.upper str2.up...
Python字串操作
python如何判斷乙個字串只包含數字字元 python 字串比較 下面列出了常用的python實現的字串操作 strcpy sstr1,sstr2 sstr1 strcpy sstr2 sstr1 sstr1 strcpy2 print sstr2 strcat sstr1,sstr2 sstr1...
python字串操作
在 python 有各種各樣的string操作函式。在歷史上string類在 python 中經歷了一段輪迴的歷史。在最開始的時候,python 有乙個專門的string的module,要使用string的方法要先import,但後來由於眾多的 python 使用者的建議,從 python 2.0開...