Python 字符串操作(替换、删除、截取、复制、连接等)
上一篇 /
下一篇 2013-05-20 10:54:17
/ 个人分类: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'
print theString.strip( 'say' )
|
D:M:[ T%zDJKk0theString依次被去除首尾在['s','a','y']数组内的字符,直到字符不在数组内。所以,输出的结果为:
.d;e0^REsTO0yes no 51Testing软件测试网"[`$k!p0\:@Q|oY]
比较简单吧,lstrip和rstrip原理是一样的。51Testing软件测试网#_`
V+l3a*h3RH
vc*Pzy5b @@WO$b#F0注意:当没有传入参数时,是默认去除首尾空格的。
}kA4J
Wa0theString = ' 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') Z q {M
C,r&Ja0print withoutFrontEndSpaceString.rstrip('say')
|
51Testing软件测试网w{0k3x)d!L运行结果:51Testing软件测试网5[1v{9v
U&D,^H R
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[2O Dulb7@ jp
2FC9jH4PX6|0
p ?
XD@
v0