删除某些字符内的字符串

时间:2018-10-30 00:43:16

标签: python string list

如果我有一个字符串,例如

  

string =“此文本为&此文本也是程序未知&未知”

如何删除&字符中的所有内容?

3 个答案:

答案 0 :(得分:2)

使用find的简单方法:

s = "This text is &This text is also unknown &unknown to the program"

start = s.find('&')
end = s.find('&', start+1)
result = s[:start] + s[end+1:]
print(result)

输出

This text is unknown to the program

答案 1 :(得分:2)

您可以使用regedit将其删除(我想您还希望&消失):

import re
string = "This text is &This text is also unknown &unknown to the program"
sEdit = re.sub('&.*&', '', string)
print(sEdit)

答案 2 :(得分:0)

replace + find

print(string.replace(string[string.find('&'):string.find('&',string.find('&')+1)+1],''))

输出:

This text is unknown to the program