如果我有'the quick brown fox'
这样的字符串,如何从原始字符串中删除连续的单词,例如“quick brown”以获取'the fox'
?我试过了strip()
,但没有用,我也不确定还能做些什么。
答案 0 :(得分:3)
使用str.replace()
:
In [2]: strs='the quick brown fox'
In [3]: strs.replace('quick brown','')
Out[3]: 'the fox'
In [4]: " ".join(strs.replace('quick brown','').split())
Out[4]: 'the fox' #single space between 'the' and 'fox'
help()
上的 str.replace()
:
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
答案 1 :(得分:1)
答案 2 :(得分:-1)
mystring.replace(" quick brown ", " ", 1)