擅长:python、mysql、java
<p>字符串对象包含一个方法<a href="https://docs.python.org/2/library/stdtypes.html?highlight=partition#str.partition" rel="nofollow">^{<cd1>}</a>,它正是为这种用途而设计的。你知道吗</p>
<pre><code>>>> s = 'this is a delicious apple pie'
>>> pre, target, post = s.partition('delicious')
>>> pre
'this is a '
>>> target
'delicious'
>>> post
' apple pie'
</code></pre>
<p>如果目标子字符串未包含在完整字符串中,则返回的元组在第2和第3个位置将有空字符串:</p>
<pre><code>>>> s2 = 'this is an apple pie'
>>> s2.partition('delicious')
('this is an apple pie', '', '')
</code></pre>