Find a character from a string without using find method?
<p>As I am a newbie ...  not understanding why it's not working properly ?
expecting  the cause and solution and also tips
thanks</p>

<pre><code>str = "gram chara oi ranga matir poth"
find ='p'
l = len(str)
for x in range(0,l):
    if str[x]==find:         
        flag = x
        #break
if flag == x:
    print "found in index no",flag
else:
    print "not found in there"
</code></pre>

<p>You're trying to have flag be the location of the index of the (first) matching character. Good!</p>

<pre><code>if str[x]==find:
  flag = x
  break
</code></pre>

<p>But how to tell if nothing was found?  Testing against x won't help!  Try initializing flag to a testable value:</p>

<pre><code>flag = -1
for x in range(0,l):
    if str[x]==find:         
        flag = x
        break
</code></pre>

<p>Now everything is easy:</p>

<pre><code>if flag != -1:
    print "found in index no",flag
else:
    print "not found in there"
</code></pre>

python string