Top-rated recipes tagged "substring"http://code.activestate.com/recipes/tags/substring/top/2011-08-19T05:17:00-07:00ActiveState Code RecipesSearch sequences for sub-sequence (Python)
2011-08-19T05:17:00-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577850-search-sequences-for-sub-sequence/
<p style="color: grey">
Python
recipe 577850
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/searching/">searching</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/sublist/">sublist</a>, <a href="/recipes/tags/substring/">substring</a>).
</p>
<p>The list and tuple index() method and <code>in</code> operator test for element containment, unlike similar tests for strings, which checks for sub-strings:</p>
<pre class="prettyprint"><code>>>> "12" in "0123"
True
>>> [1, 2] in [0, 1, 2, 3]
False
</code></pre>
<p>These two functions, search and rsearch, act like str.find() except they operate on any arbitrary sequence such as lists:</p>
<pre class="prettyprint"><code>>>> search([1, "a", "b", 2, 3], ["b", 2])
2
</code></pre>