Is there a string function that will tell me...

Paul Brian pbrian at demon.net
Thu Apr 19 11:47:36 EDT 2001


Terrence,

I do not know of any such in python, but the following may help:

import string
def test(str, sub, occr):
    #act like fortran AT function
    z = 0
    count = 0
    while count < occr:
        x = string.find(str,sub,z)
        if x == -1:
            break
        else:
            count = count + 1
            z = x + 1
    #this should now be index
    return x

s = 'terrence'
a = test(s,'e',3)
print a

gives you index of the 3rd 'e'

I am vaguely aware of something in re that does what you are after, but I
cannot find it.
hope it helps...

"Terrence Spencer" <terrence at mactexas.com> wrote in message
news:bYCD6.132818$wx.21739668 at typhoon.austin.rr.com...
> In the Foxpro language the is a function called:
>
>     AT(SearchingFor,StringToSearch,Count)
>
> So if I want to find out what location the 3rd "e" is in the string
> "terrence" I would say:
>
> ?at("e","terrence",3)
>
> and that would return 7, the location of the 3rd "e".
>
> Does python have something simular?
>
> Thanks.
>
> --
>
> ---------------------------------------------------------
> Terrence P. Spencer
> Director of Information Technologies
> Municipal Advisory Council of Texas
>
>





More information about the Python-list mailing list