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

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

我的栏目
  • 栏目:Python
 

去空格及特殊符号

)X;udG8q}!H%Q*|@0Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。51Testing软件测试网/w{8qcm7CS

51Testing软件测试网 Fi2QbG6{w z

这三个函数都可传入一个参数,指定要去除的首尾字符。

H-m w5y2I7d+SH0
s.strip().lstrip().rstrip(',')
theString='saaaay yes no yaaaass'
printtheString.strip('say')

~/u`'d%y#V(W0theString依次被去除首尾在['s','a','y']数组内的字符,直到字符不在数组内。所以,输出的结果为: 
Wq F/K(XI4J cd5[0yes no 
:^#^)O*D4l4L'T:j:u8`:^0比较简单吧,lstrip和rstrip原理是一样的。51Testing软件测试网vl7{/h Z fNrz

+y0^ur_,h-S#H q0注意:当没有传入参数时,是默认去除首尾空格的。 

_+fHGU ojEN*G0
theString = '   saaaay yes no yaaaass   '
}&Yb a$O9Bv _0withoutFrontEndSpaceString = theString.strip().lstrip().rstrip(',');    #去首位空格,右去逗号。 5/20/2013 11:44:05 AM
0R)H/IWB0print withoutFrontEndSpaceString;                   #saaaay yes no yaaaass
ig-mR]U^"C ^0print withoutFrontEndSpaceString.strip('say');       #withoutFrontEndSpaceString依次被去除首尾在['s','a','y']数组内的字符,直到字符不在数组内。51Testing软件测试网lX\,G.Q\8L5N
print withoutFrontEndSpaceString.strip('say ') #say后面有空格
Q K@&q'C\&Rl(Uq0print withoutFrontEndSpaceString.lstrip('say')
/y^P9mAb8B^$^1Y;i0print withoutFrontEndSpaceString.rstrip('say')

u`z"r#H^JY|'V0运行结果:

r(L H)f(D2L9P0

&BAt]`0saaaay yes no yaaaass51Testing软件测试网5c;N7fc@x
 yes no51Testing软件测试网W~X \.W
es no
:Vi8}8nA0 yes no yaaaass51Testing软件测试网7f]_6Xxh
saaaay yes no 51Testing软件测试网5EZ/fq2A;I:L Y

1| V9jsn/r _-mp0 

N`)yX'E Pc%Wi_051Testing软件测试网a/b0dM$qb

格式化输出---- %s; %d; %x; %o; %f;51Testing软件测试网j2Y0Dhs5FH4g,I,n
strHello = "the length of (%s) is %d" %('Hello World', len('Hello World'))51Testing软件测试网}2kw$k0F@
print strHello51Testing软件测试网 F&gC.Mx7Q.d

?TtH y6c:AN0 51Testing软件测试网+~]*[6KB8ux,K ?

复制字符串

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

 

连接字符串

#strcat(sStr1,sStr2)
sStr1 = 'strcat'51Testing软件测试网(P$~$w%e&X
sStr2 = 'append'51Testing软件测试网!H"y\X4u@$uLr c
sStr1 += sStr251Testing软件测试网"G |FE FK [q
print sStr1
 

查找字符

#strchr(sStr1,sStr2)#< 0 为未找到
sStr1 ='strchr'
ju;[!C:_zf0sStr2='s'
!GPAbp5A.N'[4P:T0nPos=sStr1.index(sStr2)
1o}6u&RH@0print 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