string = "how to dye my brunet hair to blonde? "
list = ['how', 'how to',"bananas"]
list.sort(key=len,reverse=True) # sort by decreasing length
for sample in string, "bananas taste swell", "how do you do?":
for beginning in list:
if sample.startswith(beginning):
print sample[len(beginning):]
break
else: # None of the beginnings matched
print sample
以这种方式使用时,^{} 将用第二个参数替换所有出现的第一个参数。它还接受可选的第三个参数,这将限制替换的数量。例如,如果您只希望替换第一个出现的“how to”,这将非常有用。在
这样可以确保更换只在开始时进行。不过,效率并不高。如果你能清楚地知道你想要做什么,也许会有帮助。在
相关问题 更多 >
编程相关推荐