Python: Function to return index starting from the end for a string
<p>Is there a function to return the end index instead of the start index of a substring. For example, for</p>

<pre><code>string = 'the rain in spain stays mainly in the plain'
</code></pre>

<p><code>string.foo('mainly ')</code> returns 31, instead of having to use <code>string.index('mainly ')+len('mainly ')</code>? </p>

<p>It can be easily implemented in two lines:</p>

<pre><code>def get_sub_end(s, sub):
    f = s.find(sub)
    return f if f == -1 else f + len(sub)
</code></pre>

<p>Usage:</p>

<pre><code>test_str = 'the rain in spain stays mainly in the plain'

if __name__ == '__main__':
    print get_sub_end(test_str, "mainly ")  # &gt;&gt;&gt; 31
    print get_sub_end(test_str, "undefined ")  # &gt;&gt;&gt; -1
</code></pre>

python string indexing slice