[英]Python - How do i delete more than one character on python using slice method, find and rfind
I need some help in removing two sets of curly brackets with string.Right now it's only removing one set.我需要一些帮助来删除两组带有字符串的大括号。现在它只删除一组。 I tired to use find and rfind to find the first and last occurence of the string index and using slice method to cut out the curly brackets but it can only remove one set of it.
我厌倦了使用 find 和 rfind 来查找字符串索引的第一次和最后一次出现,并使用 slice 方法剪切大括号,但它只能删除一组。
It should work something like that它应该像那样工作
Input:<\/strong> {fefeef}this{feofeow}.4849输入:<\/strong> {fefeef}this{feofeow}.4849
Output<\/strong> this.4849输出<\/strong>this.4849
Here is my code这是我的代码
def help_mmtf():
while True:
user_input = input("Filename?")
text_input = str(user_input)
if (text_input !=""):
word = text_input
index1 = word.find("{")
index2 = word.find("}")
print(index1)
print(index2)
index3 = word.rfind("{")
index4 = word.rfind("}")
print(index3)
print(index4)
splice_input = text_input[:index1] + text_input[index4+1:]
print(f'"{splice_input}"')
else:
break
You can use sub<\/code>
您可以使用
sub<\/code>
import re
def help_mmtf():
while True:
user_input = input("Filename?")
text_input = str(user_input)
if (text_input != ""):
word = text_input
splice_input = re.sub("\{.*?\}", "", word)
print(f'"{splice_input}"')
else:
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.