samrobertsmith at gmail.com
Sun Oct 28 02:08:06 CET 2007
On 10/27/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
> "linda.s" <samrobertsmith at gmail.com> 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
def nth_index(aString, aChar, N):
n = 0
for i in range(N):
n = aString.index(aChar, n+1)
More information about the Tutor