Neatest way to do a case insensitive "in"?

T

tinnews

What's the neatest way to do the following in case insensitive fashion:-

if stringA in stringB:
bla bla bla

I know I can just do:-

if stringA.lower() in stringB.lower():
bla bla bla

But I was wondering if there's a neater/easier way?
 
A

Albert Hopkins

What's the neatest way to do the following in case insensitive fashion:-

if stringA in stringB:
bla bla bla

I know I can just do:-

if stringA.lower() in stringB.lower():
bla bla bla

But I was wondering if there's a neater/easier way?

How is "if stringA.lower() in stringB.lower():" complex/messy?
 
T

tinnews

Albert Hopkins said:
How is "if stringA.lower() in stringB.lower():" complex/messy?
Well I guess I was just looking for "incase" a bit like "strcasecmp"
in C.
 
M

MRAB

What's the neatest way to do the following in case insensitive fashion:-

if stringA in stringB:
bla bla bla

I know I can just do:-

if stringA.lower() in stringB.lower():
bla bla bla

But I was wondering if there's a neater/easier way?
Not unless someone writes a patch to add a case-insensitive 'find'
method to str.
 
S

Steve Holden

aiwarrior said:
I do agree with you but what if your really insist and perhaps
subclass the str and override the __contains__(x) method?
But then objects have to be explicitly created as subclasses of str.

regards
Steve
 
S

Steve Holden

aiwarrior said:
I do agree with you but what if your really insist and perhaps
subclass the str and override the __contains__(x) method?
But then objects have to be explicitly created as subclasses of str.

regards
Steve
 
J

Jervis Whitley

   if stringA.lower() in stringB.lower():
       bla bla bla

from string import lower

if lower(stringA) in lower(stringB):
# was this what you were after?

Cheers,

Jervis
 
A

Albert Hopkins

from string import lower

if lower(stringA) in lower(stringB):
# was this what you were after?

This is analogous to standing behind a perfectly functioning automobile
and pushing it everywhere you want to go.
 
J

Jervis Whitley

This is analogous to standing behind a perfectly functioning automobile
and pushing it everywhere you want to go.

It is an alternative solution. That is all.

Cheers,

Jervis
 
A

Albert Hopkins

It is an alternative solution. That is all.

I agree that it's an alternative. There are a number of alternatives.
However the OP was asking for a "neater/easier" alternative. I argue
that introducing an external module/function to do the exact same thing
as a built-in type's method doesn't exactly qualify as a "neater/easier"
alternative.

In fact, unless you are using a *very* old version of python, if you
look at the implementation of string.lower(s) it simply returns
s.lower().
 
J

Jervis Whitley

I agree that it's an alternative. There are a number of alternatives.
However the OP was asking for a "neater/easier" alternative. I argue
that introducing an external module/function to do the exact same thing
as a built-in type's method doesn't exactly qualify as a "neater/easier"
alternative.

I argue that it is this functional approach looks visually clean and
hence may appeal
to a readers sense of "neatness" or aesthetics. No one is right or
wrong in this case,
they are both fine solutions.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top