我需要一个能够查看字符串并删除两个字符串"About DoctorTardi"
和"Stats for Project Ares"
之间的所有内容的函数。
答案很可能是面对着我......
答案 0 :(得分:2)
使用str.partition
或str.rpartition
:
>>> s = "aaa About DoctorTardi this Stats for Project Ares bbb"
>>> head, sep1, x = s.partition("About DoctorTardi")
>>> _, sep2, tail = x.partition("Stats for Project Ares")
>>> head + sep1 + sep2 + tail
'aaa About DoctorTardiStats for Project Ares bbb'
答案 1 :(得分:1)
def remake_string(initialString, startPhrase, endPhrase):
startingIndex = initialString.rfind(startPhrase)e #finds the highest index of the phrase
endingIndex = initialString.find(endPhrase)# finds the lowest index of the phrase
newString = initialString[:startingIndex] + initialString[endingIndex:] '''
removes everything in the midding by starting getting everything up to the end of
the startPhrase and adding it to from the endPhrase on '''
return newString
答案 2 :(得分:0)
x = "About DoctorTardi bla bla Stats for Project Ares"
x = x[:17]+x[25:]
print x
将打印
关于ProjectTardi项目战神统计数据
x[:17]
是x的子字符串,从开头到第17个字母。
x[25:]
是x的子字符串,从第25个字母到结尾。
x[17:25]
是x的子字符串,从第17个字母到第25个字母
信。