<p>引自<a href="http://docs.python.org/2/library/stdtypes.html#str.strip" rel="nofollow">^{<cd1>} docs</a></p>
<blockquote>
<p>Return a copy of the string with the leading and trailing characters
removed. The chars argument is a string specifying the set of
characters to be removed. If omitted or None, the chars argument
defaults to removing whitespace. <strong>The chars argument is not a prefix or
suffix; rather, all combinations of its values are stripped:</strong></p>
</blockquote>
<p>因此,它从字符串的两侧删除参数中的所有字符。你知道吗</p>
<p>例如</p>
<pre><code>my_str = "abcd"
print my_str.strip("da") # bc
</code></pre>
<p><strong>注意:</strong>您可以这样想,当它发现在输入参数字符串中找不到的字符时,它会停止从字符串中删除字符。你知道吗</p>
<p>实际上,要删除特定的字符串,应该使用<code>str.replace</code></p>
<pre><code>x = "/Users/Desktop/testspace/Hy5_Workspace/security/ios_static_analyzer/"
print x.replace('analyzer/', '')
# /Users/msecurity/Desktop/testspace/Hy5_Workspace/security/ios_static_
</code></pre>
<p>但是替换会把火柴弄得到处都是</p>
<pre><code>x = "abcd1abcd2abcd"
print x.replace('abcd', '') # 12
</code></pre>
<p>但是如果您只想删除字符串开头和结尾的单词,可以使用RegEx,如下所示</p>
<pre><code>import re
pattern = re.compile("^{0}|{0}$".format("abcd"))
x = "abcd1abcd2abcd"
print pattern.sub("", x) # 1abcd2
</code></pre>