s = 'These are the characters, from this point I want to delete everything'
当Python看到来自'时,我想删除之后的所有内容。
答案 0 :(得分:3)
我会使用index
。
def shorten(s, subs):
i = s.index(subs)
return s[:i+len(subs)]
用法:
s = 'These are the characters, from this point I want to delete everything'
print(shorten(s, 'from'))
输出:
这些是来自
的字符
答案 1 :(得分:0)
您可以使用takewhile拆分和重新加入字符串,以消费直至来自的字词:
s = 'These are the characters, from this point I want to delete everything'
from itertools import takewhile
new_s = " ".join(takewhile(lambda x: x != "from", s.split(" ")))
你也可以分开"来自"曾经和以前的角色:
s = 'These are the characters, from this point I want to delete everything'
new_s = s.split("from",1 )[0]
但是,这将分散于"来自"无论是实际的单词还是子串,可能都不是你想要的。
如果你想要一个完全匹配并处理各种不同的可能性,比如from和preiod之间没有空格,逗号等......你需要使用word boundary-的正则表达式:
import re
new_s = re.split(r"\bfrom\b",s, 1)[0]