2020-06-04
阅读量:
942
python:str.strip()用法
函数原型:
str.strip()
作用:删除字符串头和尾的空格,以及位于头尾的\n \t之类等等
eg1:
str = " \n \n A B C \t"
str.strip()
结果:
str = "A B C"
eg2:
str = "0000000this is string example....wow!!!0000000";
print str.strip( '0' );
结果:
this is string example....wow!!!
值得注意的是:
当str.strip()括号内为空的时候,默认删除空白符(包括'\n', '\r', '\t', ' ')
eg3:
str = 'hello world'
print str.strip( 'hello' )
print str.strip( 'hello' ).strip()
print str.strip( ' heldo ' ).strip() #sentence 1
stt = 'h1h1h2h3h4h'
print stt.strip( 'h1' ) #sentence 2
s = '123459947855aaaadgat134f8sfewewrf7787789879879'
print s.strip( '0123456789' ) #sentence 3
结果:
world
world
wor
2h3h4
aaaadgat134f8sfewewrf






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
3条评论