string.find first before location

G

Gary Wessle

Hi

I have a string like this

text = "abc abc and Here and there"
I want to grab the first "abc" before "Here"

import string
string.find(text, "Here") # <type int>

I am having a problem with the next step.

thanks
 
P

Peter Otten

Gary said:
I have a string like this

text = "abc abc and Here and there"
I want to grab the first "abc" before "Here"

import string
string.find(text, "Here") # <type int>

I am having a problem with the next step.

These days str methods are preferred over the string module's functions.
4

Peter
 
G

Gary Wessle

Peter Otten said:
These days str methods are preferred over the string module's functions.

4

Peter

and what about when python 3.0 is released and those depreciated
functions like find and rfind are not supported. is there another
solution which is more permanent?
 
P

Peter Otten

Gary said:
and what about when python 3.0 is released and those depreciated
functions like find and rfind are not supported. is there another
solution which is more permanent?

I think the functions may go away, the methods will stay; so I'm confident
the above will continue to work.

Peter
 
S

Serge Orlov

Peter said:
I think the functions may go away, the methods will stay; so I'm confident
the above will continue to work.

find and rfind methods are in danger too. AFAIR they are to be replaced
by partion and rpartition methods. People who are worried about future
can continue to use index and rindex
 
P

Peter Otten

Serge said:
find and rfind methods are in danger too. AFAIR they are to be replaced
by partion and rpartition methods. People who are worried about future
can continue to use index and rindex

I really should read those PEPs before posting.

And just as I was starting to complain I noted that my original reply to
Gary is buggy -- excluding the last letter in the string from the rfind()
search if "Here" is not found is probably never the desired behaviour. So
r/index() is indeed superior here.

Peter
 
G

Gary Wessle

Serge Orlov said:
find and rfind methods are in danger too. AFAIR they are to be replaced
by partion and rpartition methods. People who are worried about future
can continue to use index and rindex

except with index and rindex, if the search string is not present,
they return a
[
Traceback (most recent call last):
ValueError: substring not found
]

ps. is there a online doc or web page where one enters a method and it
returns the related docs?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top