"linda.s" <[EMAIL PROTECTED]> wrote

>I have a string a= "qq,eee,rrr".
>>>> a.index(",")
> 2
> It is the position of the first "," in a.
> Is that possible to find the Nth "," in a if a is a very long string
> and I need know the position of the Nth ","?

Yes but not directly.
index takes a couple of optional parameters

    S.index(sub [,start [,end]]) -> int

So by repeating the index call in a loop using the previous
index as youur start position you an get it

untested pseudo code

def nth_index(aString, aChar, N):
    n = 0
    for i in range(N):
        n = aString.index(aChar, n)
    return n

This should return the Nth index value or raise a
ValueError if less than N occurences exist

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to