string.find for case insensitive search

J

Johny

Is there a good way how to use string.find function to find a
substring if I need to you case insensitive substring?
Thanks for reply
LL
 
D

Duncan Booth

Johny said:
Is there a good way how to use string.find function to find a
substring if I need to you case insensitive substring?

s.lower().find(substring.lower())
 
D

Don Morrison

string.find is deprecated as per the official python documentation.

take a look at the "re" module
 
L

Larry Bates

Johny said:
Is there a good way how to use string.find function to find a
substring if I need to you case insensitive substring?
Thanks for reply
LL
Maybe something like:

x="my string I'm going to SEarCH"
hasword='SEARCH' in x.upper()
location=x.upper().find('SEARCH')

print hasword
True

print location
23


-Larry
 
R

Robert Kern

Don said:
lower() is also deprecated :) oh well

The string method .lower() is not deprecated.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
D

Duncan Booth

Don Morrison said:
lower() is also deprecated :) oh well
No. RTFM and don't top post.

The functions such as lower() and find() in the string *module* are
deprecated. The methods are what you should use.
 
D

Don Morrison

My apologies, I confused the built-in "str" with the module "string".
I was reading from the section of the 2.4.4 docs called: 4.1.4
Deprecated string functions
 
B

Bruno Desthuilliers

Johny a écrit :
Is there a good way how to use string.find function to find a
substring if I need to you case insensitive substring?

"abCdZEd".lower().find("BcD".lower())
 
D

Don Morrison

Don said:
but the str.find() method isn't.

A possibility.

regards
Steve

Thank you everyone. :) Johny did say "string.find" in his message, not
"str.find", but continue to proceed with flogging. thank you!
 
S

Steven D'Aprano

string.find is deprecated as per the official python documentation.

take a look at the "re" module

Regular expressions are way, WAY overkill for a simple find. Just use
the string methods. Instead of this:

import string
string.find("Norwegian Blue", "Blue")


just do this:

"Norwegian Blue".find("Blue")

For case insensitive find:

"Norwegian Blue".lower().find("Blue".lower())

(or better still, turn it into a function).
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top