def sstrip(a):
b=raw_input("enter the string to be stripped off")
i=a.strip(b)
print i
k=raw_input("enter the string")
sstrip(k)
output:
enter the string - is it available?
enter the string to be stripped off - is
t available?
在上面的程序中,i 是两个字符串 is 的一部分,并且 it..“it” 是一个中间词。其中 i 也被剥夺了。
有人可以帮助我吗
str.strip
按字符剥离(不断删除,直到到达参数中不存在的字符),问题是您之前包含了空格is
在您的输入中:
>>> 'is it'.strip('is')
' it'
>>> 'is it'.strip('- is')
't'
如果你真正想做的是slice从较大字符串的开头或结尾删除子字符串,那么您可以使用以下命令:
def rcut(a, b):
return a[:-len(b)] if a.endswith(b) else a
def cut(a, b):
a = rcut(a, b)
return a[len(b):] if a.startswith(b) else a
print cut('- is it available?', '- is')
# it available?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)