case insensitive comparison operator ?

W

Will McGugan

What is the best way to do a simple case insensitive comparison in Python?

As far as I can tell, the only option is to do str1.lowercase() ==
str2.lowercase() (or uppercase). But that strikes me as quite verbose
for such a common operation, and possibly inneficient since it would
create and destroy 2 tempory objects. Perhaps there should be a
dedicated function in the string object, or (+1) an operator?

Apologies if I have missed something. I haven't done a great deal of
work int Python yet.


Will McGugan
 
P

Peter Hansen

Will said:
What is the best way to do a simple case insensitive comparison in Python?

As far as I can tell, the only option is to do str1.lowercase() ==
str2.lowercase() (or uppercase).

Actually, it's str1.lower() == str2.lower().
But that strikes me as quite verbose

Luckily it's not that verbose after all! ;-)
for such a common operation,

Nor that common! If you need to do this more than once in an
application, you can easily create a simple compareCaseInsensitive()
function that hides the above "excessive verbosity" and you'll
never notice it again. Furthermore, in most cases you've already
stored one of those two strings, so if you lower() it when you
store it, you only need to do "str1 == str2.lower()" when you
are comparing later on. At least that's the use case I've
seen to be "common", and I've written more Python code than you
have so there. ;-)
and possibly inneficient since it would create and destroy 2
> tempory objects.

Python creates and destroys a lot more temporary objects than you
might imagine, and still gets by. In fact, the function calls
involved probably have much higher overhead (from setting up
the stack frame) than the object creation... Python often upsets
conventional ideas of where hotspots will be.
Apologies if I have missed something. I haven't done a great deal of
work int Python yet.

Worrying about possible inefficiencies before you've even done
much coding in a language is a clear case of premature
optimization. Just write some code, learn to use and love Python,
and if you still really want this, come back later and suggest
it again. ;-)

-Peter
 
M

Max M

Will said:
What is the best way to do a simple case insensitive comparison in Python?

As far as I can tell, the only option is to do str1.lowercase() ==
str2.lowercase() (or uppercase). But that strikes me as quite verbose
for such a common operation, and possibly inneficient since it would
create and destroy 2 tempory objects. Perhaps there should be a
dedicated function in the string object, or (+1) an operator?


How else could it be done?

You could wrap it in a function, and reuse the code if you want to. That
way you can imagine that it never happens ;-)

def cic(x,y);
"Case insensitive compare"
return x.lower() == y.lower()

But you cannot really do it any more efficient.

Forget your worries. It's probably just a case of premature optimisation.

Naturally there are special cases where you must do it more efficiently.
But they would probably need to be solved based on the specific problem.

regards Max M
 

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

Latest Threads

Top