strip()函数用法

strip的意思是剥夺。

strip()函数作用是操作字符串。

按一定规则剥夺字符串内容,移除头尾,不移除中间。

strip([chars])函数参数是字符序列,注意:字符序列。

1 - strip()不传参

str = '     helloworld     '
print(str.strip())

运行结果:

helloworld

2 - strip()传一个字符

str = '009876000dfd7647000000'
print(str.strip('0'))

运行结果:

9876000dfd7647

3 - strip()传一个字符序列

str = '12sdfjladsjfalksdjw12'
print(str.strip('1w2'))

运行结果

sdfjladsjfalksdj

头尾只要有字符序列内字符,都去掉。

猜你喜欢

转载自www.cnblogs.com/gnuzsx/p/12657263.html
今日推荐