对于上述问题,我尝试了以下代码。
def removesubstr(substr,x):
a = len(substr)
m = ""
count = 0
b = 0
c= len(x)
while count <c:
for i in x:
if x[b:b+a] == substr:
count = count +1
m =m +""
b=b+a
else:
b= b+1
m = m + i
count = count +1
return m
print (removesubstr('ara','jayaram'))
我正在寻找类似“ jaym”的输出。但我意识到,由于其循环了3个步骤。python仍将最后3个完整字符[4:7]视为有效,并输出'jayram'。我如何改变才能得到“ jaym”?
tempstr='jayaram'
tempstr=tempstr.replace("ara", "",1);
您可以使用python内置替换。可以将其替换为空白
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句