On 10/27/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
> "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
>
should be:
def nth_index(aString, aChar, N):
n = 0
for i in range(N):
n = aString.index(aChar, n+1)
return n
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor