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><code>if flag == x</code> looks strange. What value of <code>x</code> are you expecting there?</p>

<p>I'd suggest instead initializing <code>flag</code> to <code>None</code> and then checking for it not being <code>None</code> at the end of the loop.</p>

<pre><code>flag = None # assign a "sentinel" value here
str = "gram chara oi ranga matir poth"
find = 'p'
for x in range(len(str)):
    if str[x] == find:         
        flag = x
        # Leaving out the break is also fine. It means you'll print the
        # index of the last occurrence of the character rather than the first.
        break
if flag is not None:
    print "found in index no", flag
else:
    print "not found in there"
</code></pre>

python string