[Tutor] String comparison
Yigal Duppen
yduppen@xs4all.nl
Thu, 8 Aug 2002 12:04:00 +0200
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi Anand,
First of all, as stated by many other participants in this list, tutor is a
list for newbies, so questions here are never stupid :)
Python has no command for comparing strings irrespective of case; the usual
approach is to lowercase (or uppercase) the strings before comparing them:
>>> a = "a"
>>> b = "B"
>>> a < b
0
>>> a.lower() < b.lower()
1
>>> a.upper() < b.upper()
1
>>> a
'a'
>>> b
'B'
As you can see, both upper and lower return copies; they leave the original
strings intact. Strings are immutable objects in Python.
And just in case you might be worried about performance of this approach, as
opposed to some compareIgnoreCase() function: don't be worried. Python's
string operations are highly optimized and every Pythoneer reading the above
code understands :)
Hope this helps!
YDD
- --
http://www.xs4all.nl/~yduppen
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
iD8DBQE9UkIQLsKMuCf5EdwRAvxfAKDDxs22gDX/eYHJexTXvJzvP5RYlgCgsl/U
8XczrynG9KjaUIpjClDdddw=
=d1pI
-----END PGP SIGNATURE-----