case insensitive comparison operator ?
Peter Hansen
peter at engcorp.com
Wed Aug 18 07:27:01 EDT 2004
More information about the Python-list mailing list
Wed Aug 18 07:27:01 EDT 2004
- Previous message (by thread): case insensitive comparison operator ?
- Next message (by thread): case insensitive comparison operator ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Will McGugan wrote: > 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
- Previous message (by thread): case insensitive comparison operator ?
- Next message (by thread): case insensitive comparison operator ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list