擅长:python、mysql、java
<p><code>.strip</code>方法并不像您想象的那样:</p>
<pre><code>Docstring:
S.strip([chars]) -> string or unicode
Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
</code></pre>
<p>所以你要说的是“从这个字符串的开头或结尾删除/,o,v,e,r,f,l,o,w。”</p>
<p>相反,尝试在<code>/</code>上拆分,获取除最后一个元素之外的所有元素,然后重新连接:</p>
<pre><code>In [12]: '/'.join("/stack/overflow".split('/')[:-1])
Out[12]: '/stack'
</code></pre>
<p>如果实际上只想删除子字符串'/overflow',可以执行以下操作:</p>
<pre><code>In [15]: "/stack/overflow".replace('/overflow', '')
Out[15]: '/stack'
</code></pre>