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

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

我的栏目
  • 栏目:Python
 

去空格及特殊符号

51Testing软件测试网;Y\^PT^

Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。51Testing软件测试网$g]/x+k*m

51Testing软件测试网D#X$n p-Y4cD

这三个函数都可传入一个参数,指定要去除的首尾字符。51Testing软件测试网S)qq(tf'm

s.strip().lstrip().rstrip(',')
theString='saaaay yes no yaaaass'
printtheString.strip('say')
51Testing软件测试网N6b'F*ca

theString依次被去除首尾在['s','a','y']数组内的字符,直到字符不在数组内。所以,输出的结果为: 
\P l n#Be!Da u0yes no 51Testing软件测试网2c%}V;J_aU r
比较简单吧,lstrip和rstrip原理是一样的。51Testing软件测试网v-t\}6jhV

51Testing软件测试网0YA\8\gOWM*g

注意:当没有传入参数时,是默认去除首尾空格的。 51Testing软件测试网~(c9T E tq0?9a

theString = '   saaaay yes no yaaaass   '
:Y"T,g sbjL0withoutFrontEndSpaceString = theString.strip().lstrip().rstrip(',');    #去首位空格,右去逗号。 5/20/2013 11:44:05 AM
bC3HM G2Y HN0print withoutFrontEndSpaceString;                   #saaaay yes no yaaaass
:}*|(N+C%nGj0print withoutFrontEndSpaceString.strip('say');       #withoutFrontEndSpaceString依次被去除首尾在['s','a','y']数组内的字符,直到字符不在数组内。
#hL,|7?`Y0print withoutFrontEndSpaceString.strip('say ') #say后面有空格
[.^5@ Ep/K0print withoutFrontEndSpaceString.lstrip('say')51Testing软件测试网.J'k$q9\)hA
print withoutFrontEndSpaceString.rstrip('say')
51Testing软件测试网DpMV N2Qhp*[/v

运行结果:

U*@#`K"v]$iv051Testing软件测试网k(v ?)p{

saaaay yes no yaaaass51Testing软件测试网6m$o&o WI"j6w
 yes no
&i6Vd?{o0es no
%~2CKb@fD0 yes no yaaaass51Testing软件测试网$a~`T3DY]+a3h!Ef
saaaay yes no 

4uD8K5]2CK051Testing软件测试网Ku Rlz}|V7bT0_

 

Lu;L@sS&d051Testing软件测试网2M@t)x+L

格式化输出---- %s; %d; %x; %o; %f;51Testing软件测试网2L E ] H7dU$n$sl
strHello = "the length of (%s) is %d" %('Hello World', len('Hello World'))
8|9\AL!P ~+m*L D$nV0print strHello

2C2T zR6i|_+bW|051Testing软件测试网Z)p8jL2`8P,WDM"A

 

XF7Qe8V0kM0

复制字符串

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

 

连接字符串

#strcat(sStr1,sStr2)
sStr1 = 'strcat'51Testing软件测试网e _'c	Krk!h
sStr2 = 'append'51Testing软件测试网/l%y+\*Z6tH(fi
sStr1 += sStr2
d)y#Em0x+c0print sStr1
 

查找字符

#strchr(sStr1,sStr2)#< 0 为未找到
sStr1 ='strchr'51Testing软件测试网hXMu|,cT
sStr2='s'51Testing软件测试网0g8Ct.w.U#W~
nPos=sStr1.index(sStr2)51Testing软件测试网2Z(xY0z ` B"NI
print 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