Python 字符串操作(替换、删除、截取、复制、连接等)

上一篇 / 下一篇  2013-05-20 10:54:17 / 个人分类:Python

我的栏目
  • 栏目:Python
 

去空格及特殊符号

(uH0BQu"F0d$b0Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。

}3M[hd}6Y|~051Testing软件测试网6gsr9N:FB/N

这三个函数都可传入一个参数,指定要去除的首尾字符。51Testing软件测试网!cUZuY$u^7?Q

s.strip().lstrip().rstrip(',')
theString='saaaay yes no yaaaass'
printtheString.strip('say')

D:M:[ T%zDJKk0theString依次被去除首尾在['s','a','y']数组内的字符,直到字符不在数组内。所以,输出的结果为: 
.d;e0^REsTO0yes no 51Testing软件测试网"[`$k!p0\:@Q|o Y ]
比较简单吧,lstrip和rstrip原理是一样的。51Testing软件测试网#_` V+l3a*h3RH

vc*Pzy5b @@WO$b#F0注意:当没有传入参数时,是默认去除首尾空格的。 

}kA4J Wa0
theString = '   saaaay yes no yaaaass   '
(@IV%z{$]~v0withoutFrontEndSpaceString = theString.strip().lstrip().rstrip(',');    #去首位空格,右去逗号。 5/20/2013 11:44:05 AM51Testing软件测试网h*Ma@L }0{xZ
print withoutFrontEndSpaceString;                   #saaaay yes no yaaaass51Testing软件测试网5d#y8{:Cr6j
print withoutFrontEndSpaceString.strip('say');       #withoutFrontEndSpaceString依次被去除首尾在['s','a','y']数组内的字符,直到字符不在数组内。
]&j#e6V:S#u0print withoutFrontEndSpaceString.strip('say ') #say后面有空格51Testing软件测试网 gY)D@ A9A d:f
print withoutFrontEndSpaceString.lstrip('say')
Zq {M C,r&Ja0print withoutFrontEndSpaceString.rstrip('say')
51Testing软件测试网w{0k3x)d!L

运行结果:51Testing软件测试网5[1v{9v U&D,^HR

j)_$bX mH%F0saaaay yes no yaaaass51Testing软件测试网s(\j{O+O%VD%h
 yes no51Testing软件测试网3i6f x.W$U
es no51Testing软件测试网@{7jN"nt(JuW
 yes no yaaaass51Testing软件测试网}5r3fU},h
saaaay yes no 51Testing软件测试网;X#K[2ODulb7@j p

2FC9j H4PX6|0 

p ? XD@ v0

E,W;VSQL|0格式化输出---- %s; %d; %x; %o; %f;
c)lH h Z&K5S0strHello = "the length of (%s) is %d" %('Hello World', len('Hello World'))51Testing软件测试网 j.NB5RM`%UK!an
print strHello

%x Kq,O5b[ st2O{&Y0

p#I!kCP2l{4H0 

hYoyY;` m3^0

复制字符串

#strcpy(sStr1,sStr2)
sStr1 ='strcpy'
sStr2=sStr1
sStr1='strcpy2'
printsStr2

 

连接字符串

#strcat(sStr1,sStr2)
sStr1 = 'strcat'51Testing软件测试网K)JSjX
sStr2 = 'append'
ZCj%ig{?}n0sStr1 += sStr251Testing软件测试网!O7~lt7R|9u
print sStr1
 

查找字符

#strchr(sStr1,sStr2)#< 0 为未找到
sStr1 ='strchr'51Testing软件测试网$w/tu7z&R`)Q-y
sStr2='s'
&Q5a XA`.C2A0nPos=sStr1.index(sStr2)
5?;Z-|"H7_%Tr3hL0print nPos
 

比较字符串

#strcmp(sStr1,sStr2)sStr1 ='strchr'sStr2='strch'printcmp(sStr1,sStr2)
 

扫描字符串是否包含指定的字符

#strspn(sStr1,sStr2)sStr1 ='12345678'sStr2='456'#sStr1 and chars both in sStr1 and sStr2printlen(sStr1andsStr2)
 

字符串长度

#strlen(sStr1)sStr1 ='strlen'printlen(sStr1)
 

将字符串中的大小写转换

#strlwr(sStr1)sStr1 ='JCstrlwr'sStr1=sStr1.upper()#sStr1 = sStr1.lower()printsStr1
 

追加指定长度的字符串

#strncat(sStr1,sStr2,n)sStr1 ='12345'sStr2='abcdef'n= 3sStr1+=sStr2[0:n]printsStr1
 

字符串指定长度比较

#strncmp(sStr1,sStr2,n)sStr1 ='12345'sStr2='123bc'n= 3printcmp(sStr1[0:n],sStr2[0:n])
 

复制指定长度的字符

#strncpy(sStr1,sStr2,n)sStr1 =''sStr2='12345'n= 3sStr1=sStr2[0:n]printsStr1
 

将字符串前n个字符替换为指定的字符

#strnset(sStr1,ch,n)sStr1 ='12345'ch='r'n= 3sStr1= n * ch + sStr1[3:]printsStr1
 

扫描字符串

#strpbrk(sStr1,sStr2)sStr1 ='cekjgdklab'sStr2='gka'nPos= -1forcinsStr1:ifcinsStr2:
        nPos=sStr1.index(c)breakprintnPos
 

翻转字符串

#strrev(sStr1)sStr1 ='abcdefg'sStr1= sStr1[::-1]printsStr1
 

查找字符串

#strstr(sStr1,sStr2)sStr1 ='abcdefg'sStr2='cde'printsStr1.find(sStr2)
 

分割字符串

#strtok(sStr1,sStr2)sStr1 ='ab,cde,fgh,ijk'sStr2=','sStr1= sStr1[sStr1.find(sStr2) + 1:]printsStr1#或者s ='ab,cde,fgh,ijk'print(s.split(','))
 

连接字符串  ---- delimiter.join(list)

delimiter =','mylist= ['Brazil','Russia','India','China']printdelimiter.join(mylist)
 

PHP 中 addslashes 的实现

defaddslashes(s):
    d= {'"':'\\"',"'":"\\'","\0":"\\\0","\\":"\\\\"}return''.join(d.get(c, c)forcins)
 
s="John 'Johny' Doe (a.k.a. \"Super Joe\")\\\0"printsprintaddslashes(s)

只显示字母与数字

defOnlyCharNum(s,oth=''):
    s2=s.lower();
    fomart='abcdefghijklmnopqrstuvwxyz0123456789'forcins2:ifnotcinfomart:
            s= s.replace(c,'');returns;print(OnlyStr("a000 aa-b"))

TAG:

 

评分:0

我来说两句

Open Toolbar